Sql代碼: 1 select t.user_name, 2 sum(decode(t.course, '語文', score,null)) as CHINESE, 3 sum(decode(t.course, '數學', score,null)) as MATH, 4 sum(decode(t.course, '英語', score,null)) as ENGLISH 5 from test_tb_grade t 6 group by t.user_name 7 order by t.user_name 1.三、延伸
若是要實現對各門功課的不一樣分數段進行統計,效果圖以下:
具體的實現sql以下: Sql代碼:
01 select t2.SCORE_GP, 02 sum(decode(t2.course, '語文', COUNTNUM,null)) as CHINESE, 03 sum(decode(t2.course, '數學', COUNTNUM,null)) as MATH, 04 sum(decode(t2.course, '英語', COUNTNUM,null)) as ENGLISH 05 from ( 06 select t.course, 07 case when t.score <60 then '00-60' 08 when t.score >=60 and t.score <80 then '60-80' 09 when t.score >=80 then '80-100' end as SCORE_GP, 10 count(t.score) as COUNTNUM 11 FROM test_tb_grade t 12 group by t.course, 13 case when t.score <60 then '00-60' 14 when t.score >=60 and t.score <80 then '60-80' 15 when t.score >=80 then '80-100' end 16 order by t.course ) t2 17 group by t2.SCORE_GP 18 order by t2.SCORE_GP 2、列轉行