當前位置:首頁 » 編程語言 » 列轉行sql寫法
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

列轉行sql寫法

發布時間: 2022-04-14 02:00:16

sql語句怎麼把列變成行

create table rotatetable1 (序號 int,company char(66),box_weight char(12),廢塑料numeric(10,2)),廢五金 numeric(10,2)),廢鋼鐵 numeric(10,2)),廢紙 numeric(10,2)),廢有色 numeric(10,2)),廢纖維 numeric(10,2)),其它 numeric(10,2)),合計 numeric(10,2)));
insert into rotatetable1(company,box_weight) select name ,'weight' from sum1 group by name;
insert into rotatetable1(company,box_weight) select name ,'box' from sum1 group by name;
update rotatetable1 set 廢塑料=box from sum1as a where a.name=rotatetable1.company and box_weight='box' and hsname='廢塑料';
update rotatetable1 set 廢塑料=weight from sum1as a where a.name=rotatetable1.company and box_weight='weight' and hsname='廢塑料';
::: :::
update rotatetable1 set 其它=box from sum1as a where a.name=rotatetable1.company and box_weight='box' and hsname='其它';
update rotatetable1 set 其它=weight from sum1as a where a.name=rotatetable1.company and box_weight='weight' and hsname='其它';
::: :::
update rotatetable1 set 合計=廢塑料+廢五金+廢鋼鐵+廢紙+廢有色+廢纖維+其它;

(所有涉及表的行列轉換均可按照這種方式實現。)

㈡ sql2008列轉行語句

http://blog.csdn.net/greystar/article/details/5660645
看看上面三種方法是不是你需要的。

㈢ sql 列轉行

先建一張轉換後的表
create table changed
(
id number,
key varchar2(10),
val varchar2(10)
)
然後執行下面的插入語句
insert all
into changed(id,key,val) values(id,'a',a)
into changed(id,key,val) values(id,'b',b)
into changed(id,key,val) values(id,'c',c)
into changed(id,key,val) values(id,'d',d)
into changed(id,key,val) values(id,'e',e)
select id,a,b,c,d,e from change;
如果需要繼續添加,就模仿上面的格式寫好了,最後別忘記commit;

㈣ sql語句 列變行 行變列

--SQL Server 2005以上版本用unpivot
use Tempdb
go
--> -->

if not object_id(N'Tempdb..#1') is null
drop table #1
Go
Create table #1([區域名稱] nvarchar(3),[小類1] int,[小類2] int,[小類3] int)
Insert #1
select N'奎文區',120,100,90
Go
select
[區域名稱],[分類],[值]
from
#1
unpivot
([值] for [分類] in([小類1],[小類2],[小類3]))b
/*
區域名稱 分類 值
奎文區 小類1 120
奎文區 小類2 100
奎文區 小類3 90
*/

㈤ SQL語句 列轉行

-- ========================= PIVOT 行列轉置 ===========================
-- 1、【行列轉置PIVOT】
declare @Score table(StuNo varchar(10), StuName varchar(50), CourseName varchar(50), Score int)
insert into @Score
select '1', 'Tom', 'Math', 80 union all
select '1', 'Tom', 'English', 82 union all
select '1', 'Tom', 'Geography', 84 union all
select '2', 'Jone', 'Math', 79 union all
select '2', 'Jone', 'English', 88 union all
select '2', 'Jone', 'Geography',86
select * from @Score

SELECT StuNo, StuName, Math, English, [Geography]
FROM @Score PIVOT (MAX(Score) FOR CourseName in (Math, English, [Geography]) ) AS ScoreList
ORDER BY StuNo

-- 2、【列行轉置UNPIVOT】
declare @ScoreList table(StuNo varchar(10), StuName varchar(50), Math int, English int, [Geography] int)
insert into @ScoreList
select '1', 'Tom', 80, 82, 84 union all
select '2', 'Jone', 79, 88, 86
select * from @ScoreList

SELECT StuNo, StuName, CourseName, Score
FROM @ScoreList UNPIVOT (Score FOR CourseName in (Math, English, [Geography]) ) AS ScorePvtTable
ORDER BY StuNo

㈥ sql語句列轉行

主要應用case語句來解決行轉列的問題
行轉列問題主要分為兩類
1)簡單的行轉列問題:
示例表:
id
sid
course
result
1
2005001
語文
80.0
2
2005001
數學
90.0
3
2005001
英語
80.0
4
2005002
語文
56.0
5
2005002
數學
69.0
6
2005002
英語
89.0
執行
select
sid,語文=isnull(sum(case
course
when
'語文'
then
result
end),0),
數學=isnull(sum(case
course
when
'數學'
then
result
end),0),
英語=isnull(sum(case
course
when
'英語'
then
result
end),0)
from
result
group
by
sid
order
by
sid
得出結果
sid
語文
數學
英語
2005001
80.0
90.0
80.0
2005002
56.0
69.0
89.0

㈦ 行轉列,列轉行怎麼做sql

/*行轉列*/
SELECT*FROM[StudentScores]/*數據源*/ASP
PIVOT
(SUM(Score/*行轉列後列的值*/)FOR
p.Subject/*需要行轉列的列*/IN([語文],[數學],[英語],[生物]/*列的值*/)
)AST
/*列轉行*/
SELECTP.ProgrectName,P.Supplier,P.SupplyNumFROM(SELECTProgrectName,OverseaSupply,NativeSupply,
SouthSupply,NorthSupplyFROMProgrectDetail
)T
UNPIVOT
(
SupplyNumFORSupplierIN
(OverseaSupply,NativeSupply,SouthSupply,NorthSupply)
)P

㈧ SQL語句如何列變行

select 欄位名按照你要求的排序就行了

select a,b,c from tableName 那麼輸出就是 abc對應的順序

㈨ SQL 語句列轉行

TB_NAME_A Count(TC_TA_ID=TA_ID && TC_TB_ID=TB_ID_A)
TB_NAME_B Count(TC_TA_ID=TA_ID && TC_TB_ID=TB_ID_B)

TC_TB_ID=TB_ID_A,TC_TB_ID=TB_ID_B 這個TB_ID_A,TB_ID_B 代表什麼?

㈩ sql 最簡單的列轉行

oracle中列傳行可用wm_concat來實現。
如test表中數據如下:
現要將name列一列顯示成行,可用如下語句:
select wm_concat(name) from test;結果: