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)
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)
浙公网安备 33010602011771号