declare @s nvarchar(1000) select @s = 'select [name] as 姓名' select @s = @s + ',sum(case when subject=''' + cast(subject as varchar) + ''' then result end) as ' + subject from CJ group by subject select @s = @s + ' from CJ group by [name]'
将数据列表转为列显示,并进行相应运算 use [websitedb] go /****** object: storedprocedure [dbo].[up_get_activity_list] scr ipt date: 06/23/2011 14:05:57 ******/ set ansi_nulls on go set quoted_identifier on go -- ============================================= -- author:
sqlserver 动态行专列 避免了数据列过多的时候大量的使用case when then......
原数据 :
UserName Subject Score
Nick 语文 80
Nick 数学 90
Nick 英语 70
Nick 生物 85
Kent 语文 80
Kent 数学 90
Kent 英语 70
Kent 生物 85
转换后: