sqlserver行轉列與列轉行(PIVOT與UNPIVOT)
阿新 • • 發佈:2019-02-08
PIVOT用於將列值旋轉為列名(即行轉列),在可以用聚合函式配合CASE語句實現
PIVOT的一般語法是:PIVOT(聚合函式(列) FOR 列 in (…) )AS P
完整語法:
table_source
PIVOT(
聚合函式(value_column)
FOR pivot_column
IN(<column_list>)
)
UNPIVOT用於將列明轉為列值(即列轉行),在SQL Server 2000可以用UNION來實現
完整語法:
table_source
UNPIVOT(
value_column
FOR pivot_column
IN(<column_list>)
)
--************************************************************************************* --**********************************行轉列********************************************* --************************************************************************************* create table course ( id int identity(1,1),--遞增1, stuNo varchar(50), courseName varchar(50), courseScore decimal ) insert into course values('02','思想政治','85.5'), ('02','數學','70'), ('02','語文','80'), ('02','物理','90'), ('02','化學','65'), ('02','英語','96') insert into course values('03','思想政治','60'), ('03','數學','65'), ('03','語文','84'), ('03','物理','70'), ('03','化學','76'), ('03','英語','54') select * from course --方法一: select ROW_NUMBER() over(order by stuNo asc) as ID,stuNo as '學號', max(case courseName when '思想政治' then courseScore else 0 end) as '思想政治', max(case courseName when '數學' then courseScore else 0 end) as '數學', max(case courseName when '語文' then courseScore else 0 end) as '語文', max(case courseName when '物理' then courseScore else 0 end) as '物理', max(case courseName when '化學' then courseScore else 0 end) as '化學', max(case courseName when '英語' then courseScore else 0 end) as '英語' from course group by stuNo --方法二: select ROW_NUMBER() over(order by a.stuNo asc) as ID,a.stuNo as '學號',MAX(a.思想政治) as '思想政治',MAX(a.數學) as '數學',MAX(a.語文) as '語文',MAX(a.物理) as '物理',MAX(a.化學) as '化學',MAX(a.英語) as '英語' from course pivot(max(courseScore) for courseName in(思想政治,數學,語文,物理,化學,英語))a group by a.stuNo
--************************************************************************************* --**********************************列轉行********************************************* --************************************************************************************* create table course1 ( ID int identity(1,1), 學號 varchar(50), 思想政治 int, 數學 int, 語文 int, 物理 int, 化學 int, 英語 int ) go select * from course1 insert into course1 select '02',86,70,80,90,65,96 union all select '03',60,65,84,70,76,54 go select * from course1 --方法一: select ROW_NUMBER() over(order by t.stuNo asc) as id,t.stuNo,t.courseName,t.courseScore from ( select 學號 as stuNo,courseName='思想政治',courseScore=思想政治 from course1 union all select 學號 as stuNo,courseName='數學',courseScore=數學 from course1 union all select 學號 as stuNo,courseName='語文',courseScore=語文 from course1 union all select 學號 as stuNo,courseName='物理',courseScore=物理 from course1 union all select 學號 as stuNo,courseName='化學',courseScore=化學 from course1 union all select 學號 as stuNo,courseName='英語',courseScore=英語 from course1 ) t order by t.stuNo,case t.courseName when '思想政治' then 1 when '數學' then 2 when '語文' then 3 when '物理' then 4 when '化學' then 5 when '英語' then 6 end --方法二: select ROW_NUMBER() over(order by a.學號 asc) as id,a.學號 as stuNo,a.courseName,a.courseScore from course1 unpivot(courseScore for courseName in(思想政治,數學,語文,物理,化學,英語))a