sqlServer数据库纵横表相互转化

sqlServer  数据库纵横表相互转化

一.纵表转横表:

1.纵表:

sqlServer数据库纵横表相互转化

2.横表:

sqlServer数据库纵横表相互转化

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数据库纵横表相互转化

2.竖表:

sqlServer数据库纵横表相互转化

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.姓名
上一篇:【转载】C#常用数据库Sqlserver通过SQL语句查询数据库以及表的大小


下一篇:jQuery DOM 元素方法(get)