行轉列2

原數據

目標數據:

 

---方法一
select t.name,
       sum(decode(t.sub, '語文',score, 0)) as "語文",
       sum(decode(t.sub, '數學',score, 0)) as "數學",
       sum(decode(t.sub, '英語',score, 0)) as "英文"
  from projects t
 group by t.name;
--方法二
with tmp_tab as
 (select t.name, t.sub,score from projects t)
select * from tmp_tab t pivot (sum(score) for sub in('語文','數學','英語'));

 

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