您好,很高兴为您解答。
看几个实例。
select
max(case name when '课桌式' then [count] else 0 end) as 课桌式,
max(case name when '分组式' then [count] else 0 end) as 分组式,
....
from
tb
create table test([name] varchar(10),[count] int)
go
insert test
select '课桌式', 100 union all
select '分组式', 360 union all
select '剧院式', 200 union all
select '董事会', 150 union all
select 'U型', 100 union all
select '宴会型', 150
select * from test
declare @sql varchar(max)
select @sql=isnull(@sql+',','')+' max(case when [name]='''+[name]+''' then [count] end) as '''+[name]+''''
from
(select [name] from test group by [name]) t
print @sql
exec('select '+@sql+' from test' )
drop table test
/*
(6 row(s) affected)
name count
---------- -----------
课桌式 100
分组式 360
剧院式 200
董事会 150
U型 100
宴会型 150
(6 row(s) affected)
U型 董事会 分组式 剧院式 课桌式 宴会型
----------- ----------- ----------- ----------- ----------- -----------
100 150 360 200 100 150
(1 row(s) affected)
*/
如若满意,请点击右侧【采纳答案】,如若还有问题,请点击【追问】
希望我的回答对您有所帮助,望采纳!
~ O(∩_∩)O~