首页 > 数据库技术 > 详细

sqlserver行转列

时间:2014-09-04 16:16:39      阅读:178      评论:0      收藏:0      [点我收藏+]

create table tb(姓名 varchar(10),课程 varchar(10),分数 int)
insert into tb values(‘张三‘,‘语文‘,74)
insert into tb values(‘张三‘,‘数学‘,83)
insert into tb values(‘张三‘,‘物理‘,93)
insert into tb values(‘李四‘,‘语文‘,74)
insert into tb values(‘李四‘,‘数学‘,84)
insert into tb values(‘李四‘,‘物理‘,94)
go
select * from tb

drop table tb

select 姓名,
max(case 课程 when ‘语文‘ then 分数 else 0 end) 语文,
max(case 课程 when ‘数学‘ then 分数 else 0 end) 数学,
max(case 课程 when ‘物理‘ then 分数 else 0 end) 物理
from tb
group by 姓名

select * from tb pivot(max(分数) for 课程 in(语文,数学,物理))b
select * from tb

sqlserver行转列

原文:http://www.cnblogs.com/lschenblog/p/3956105.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!