taotao1130

身体,革命的本钱!努力学习ing!

导航

简单的行转列

Posted on 2005-12-09 08:44  litao  阅读(108)  评论(0)    收藏  举报
Create table testRowCol (name char(10),km char(10),cj int)

insert testRowCol values('张三','语文',80)
insert testRowCol values('张三','数学',86)
insert testRowCol values('张三','英语',75)
insert testRowCol values('李四','语文',78)
insert testRowCol values('李四','数学',85)
insert testRowCol values('李四','英语',78)



select * from testRowCol




declare @sql varchar(8000)
set @sql = 'select name'
select @sql = @sql + ',sum(case km when '''+km+''' then cj end) ['+km+']'
 
from (select distinct km from testRowCol) as a
select @sql = @sql+' from testRowCol group by name'
exec(@sql)