行列转换的例子(学习笔记)

来源:互联网 发布:c语言游戏代码大全 编辑:程序博客网 时间:2024/05/15 12:14
if object_id('[huang]') is not null drop table [huang]go create table [huang]([id] int,[name] nvarchar(4),[time] nvarchar(4),[score] numeric(2,1))insert [huang]select 1,N'张三',N'1月',7.5 union allselect 1,N'张三',N'2月',8.5 union allselect 1,N'张三',N'3月',9.5 union allselect 2,N'李四',N'1月',7.1 union allselect 2,N'李四',N'2月',5.5 union allselect 2,N'李四',N'3月',7.5--------------生成数据-------------------------- declare @s nvarchar(4000)set @s=''Select     @s=@s+','+quotename([time])+N'=max(case when [time]=N'+quotename([time],'''')+N' then [Score] else 0.0 end)'from [huang] group by [time]EXEC('select [id],[NAME]'+@s+' from [huang] group by [id],[NAME]')

0 0
原创粉丝点击