当前位置: 代码迷 >> SQL >> sql话语实现表的行列倒置
  详细解决方案

sql话语实现表的行列倒置

热度:21   发布时间:2016-05-05 13:42:50.0
sql语句实现表的行列倒置
--数据表中行列转换
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
  相关解决方案