首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql话语实现表的行列倒置

2012-09-01 
sql语句实现表的行列倒置--数据表中行列转换create table stuStore(stuNum int,stuClass varchar(20),stuS

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

热点排行