sqlServer  数据库纵横表相互转化

一.纵表转横表:

1.纵表:

sqlServer数据库纵横表相互转化-LMLPHP

2.横表:

sqlServer数据库纵横表相互转化-LMLPHP

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.横表:

sqlServer数据库纵横表相互转化-LMLPHP

2.竖表:

sqlServer数据库纵横表相互转化-LMLPHP

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.姓名
04-28 09:42