Sql Pivot行轉列/列轉行應用

表結構: select * from t


需求: 獲取單科目最高分

基本辦法:

select 姓名,   

max(case 課程 when '語文' then 分數 else 0 end) 語文,  

max(case 課程 when '數學' then 分數 else 0 end) 數學,  

max(case 課程 when '物理' then 分數 else 0 end) 物理

from t

group by 姓名

結果:


Pivot 函數:

select * from t pivot(max(分數) for 課程 in (語文, 數學, 物理)) 


更多詳情, 參考:https://wenku.baidu.com/view/c0dc84182f60ddccda38a0cc.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章