表結構: 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