首页 > 其他分享 >交叉表应用示例

交叉表应用示例

时间:2022-11-17 12:13:17浏览次数:49  
标签:Province 李三 varchar Name unionselect 交叉 示例 应用

create table A
(
Name varchar(10),
Province varchar(20),
Score int
)
insert A
select '李三','四川',5 union
select '小王','四川',3 union
select '小張','廣州',3 union
select '李三','廣州',2 union
select '小張','湖南',3 union
select '李三','湖南',4

--测试
declare @s varchar(8000)
set @s = ''

select @s = @s +','+Province+'= sum(case province when'''+province+'''then score else 0 end)'
from A group by Province order by Province

exec ('select name '+@s+',sum(Score) As Total from A group by Name order by Name')

标签:Province,李三,varchar,Name,unionselect,交叉,示例,应用
From: https://www.cnblogs.com/Biyuanguang/p/16899026.html

相关文章