sqlServer数据库纵横表相互转化

sqlServer  数据库纵横表相互转化

一.纵表转横表:

1.纵表:

2.横表:

3. 代码:

select Name as '姓名',
sum(case Course when '语文' then Score else 0 end) as '语文',
sum(case Course when '数学' then Score else 0 end) as '数学',
sum(case Course when '英语' then Score else 0 end) as '英语'
 from Score group by Name

二.横表转纵表:

1.横表:

2.竖表:

3.代码:

select * from(
select Name as '姓名','语文' as '语文',Chinise as '分数' from Score1 union all
select Name as '姓名','数学' as '数学',Math as '分数' from Score1 union all
select Name as '姓名','英语' as '英语',English as '分数' from Score1)as s order by s.姓名
原文地址:https://www.cnblogs.com/zlp520/p/3602198.html