SQL Server 行轉列的實現(橫排)

2021-08-25 09:05:50 字數 4037 閱讀 2080

sql server 2000:

在一些統計報表中,常常會用到將行結果用列形式展現。我們這裡用乙個常見的學生各門課程的成績報表,來實際展示實現方法。

我們用到的表結構如下:

三張表的關係為:

現有的測試資料為:

我們需要的結果是:

sql語句如下:

declare @strsql varchar(8000) set @strsql = 'select t.stuname [姓名]' select @strsql = @strsql + ',sum(case s.sname when ''' + sname + ''' then g.[score] end) [' + sname + ']' from (select sname from [subject]) as tmp select @strsql = @strsql + ' from [score] g,[subject] s, [student] t where g.sid=s.sid and g.stuid = t.stuid group by t.stuid, t.stuname' exec(@strsql)

sql server 2005 中,已經有實現此功能的內建方法了。

sql server 2005

中新增加了兩個關係運算子 pivot/ unpivot,能夠實現表中的列轉換到行,以及行到列的轉換工作。

舉例,還是先建立測試資料表 :

create table sales.salesbymonth ( year char(4), month char(3), amount money, primary key (year, month) ) insert into sales.salesbymonth (year, month, amount) values('2004','jan', 789.0000) insert into sales.salesbymonth (year, month, amount) values('2004','feb', 389.0000) insert into sales.salesbymonth (year, month, amount) values('2004','mar', 8867.0000) insert into sales.salesbymonth (year, month, amount) values('2004','apr', 778.0000) insert into sales.salesbymonth (year, month, amount) values('2004','may', 78.0000) insert into sales.salesbymonth (year, month, amount) values('2004','jun', 9.0000) insert into sales.salesbymonth (year, month, amount) values('2004','jul', 987.0000) insert into sales.salesbymonth (year, month, amount) values('2004','aug', 866.0000) insert into sales.salesbymonth (year, month, amount) values('2004','sep', 7787.0000) insert into sales.salesbymonth (year, month, amount) values('2004','oct', 85576.0000) insert into sales.salesbymonth (year, month, amount) values('2004','nov', 855.0000) insert into sales.salesbymonth (year, month, amount) values('2004','dec', 5878.0000) insert into sales.salesbymonth (year, month, amount) values('2005','jan', 7.0000) insert into sales.salesbymonth (year, month, amount) values('2005','feb', 6868.0000) insert into sales.salesbymonth (year, month, amount) values('2005','mar', 688.0000) insert into sales.salesbymonth (year, month, amount) values('2005','apr', 9897.0000)

我們想要得到類似這樣的結果:

year jan feb mar …………..

—– ———- ———– ———–

2004 789.0000 389.0000 8867.0000 ………….

2005 7.0000 6868.0000 688.0000 …………..

用上面介紹的方法當然可以實現,但現在這裡想要的列是固定的,不是動態的,就是12個月,所以也可以這樣子來用:

select year, sum(case when month = 'jan' then amount else 0 end) as 'jan', sum(case when month = 'feb' then amount else 0 end) as 'feb', sum(case when month = 'mar' then amount else 0 end) as 'mar', sum(case when month = 'apr' then amount else 0 end) as 'apr', sum(case when month = 'may' then amount else 0 end) as 'may', sum(case when month = 'jun' then amount else 0 end) as 'jun', sum(case when month = 'jul' then amount else 0 end) as 'jul', sum(case when month = 'aug' then amount else 0 end) as 'aug', sum(case when month = 'sep' then amount else 0 end) as 'sep', sum(case when month = 'oct' then amount else 0 end) as 'oct', sum(case when month = 'nov' then amount else 0 end) as 'nov', sum(case when month = 'dec' then amount else 0 end) as 'dec' from sales.salesbymonth group by year

但這樣事實上還是相當麻煩的,現在sqlserver2005中有更方便的實現方法。

select year,[jan],[feb],[mar],[apr],[may],[jun],[jul],[aug],[sep],[oct],[nov],[dec] from ( select year, amount, month from sales.salesbymonth ) as salesbymonth pivot ( sum(amount) for month in ([jan],[feb],[mar],[apr],[may],[jun],[jul],[aug],[sep],[oct],[nov],[dec]) ) as ourpivot order by year

就是這樣,很簡單的用法,效果是完全一樣的。

我們再嘗試一下把year去掉:

select [jan],[feb],[mar],[apr],[may],[jun],[jul],[aug],[sep],[oct],[nov],[dec] from ( select amount, month from sales.salesbymonth ) as salesbymonth pivot ( sum(amount) for month in ([jan],[feb],[mar],[apr],[may],[jun],[jul],[aug],[sep],[oct],[nov],[dec]) ) as ourpivot

得到的結果是:

jan feb mar …

———- ———— ———–

796.0000 7257.0000 9555.0000 …

同乙個月份的資料累加到一起。

再給個微軟官方的例子:

SqlServer經典行轉列 討論

use cubedemo goset nocount on 銷售模組 if object id n businesscommon salemodule n u isnot null drop table businesscommon salemodule gocreate table busines...

SQL server 行轉列 列轉行

1.簡單案例 create table student sid int primary key identity 1,1 主鍵自增 sname varchar 20 學生姓名 select from student create table class cid int primary key ide...

SQL SERVER 行轉列 列轉行

第一步 建立臨時表結構 create table student 建立臨時表 stuname nvarchar 20 學生名稱 stusubject nvarchar 20 考試科目 stuscore int 考試成績 drop table student 刪除臨時表 select from stu...