sqlServer數據庫縱橫表相互轉化

sqlServer  數據庫縱橫表相互轉化sql

一.縱錶轉橫表:數據庫

1.縱表:spa

2.橫表:3d

3. 代碼:code

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

 

二.橫錶轉縱表:blog

1.橫表:數學

2.豎表:io

3.代碼:class

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.姓名
相關文章
相關標籤/搜索