sql語句實現表的行列倒置

luxika發表於2012-03-16
--資料表中行列轉換
create table stuStore
(
stuNum int,
stuClass varchar(20),
stuStores int
)--drop table stuStore
insert into stuStore values(1,'語文',100);
insert into stuStore values(1,'數學',56);
insert into stuStore values(2,'英語',90);
insert into stuStore values(2,'數學',97);
--select * from stuStore

--查詢

select stuNum,MAX(case when stuClass='語文' then stuStores else '' end) as '語文',
max(case when stuClass='數學' then stuStores else '' end) as '數學',
max(case when stuClass='英語' then stuStores else '' end) as '英語'
from stuStore group by stuNum


參考:
http://www.cnblogs.com/hongtao/archive/2010/11/16/1878630.html

相關文章