SQL結果合計與分列

結果增加一行合計

示例代碼:
select 
     case when yf is not null then yf else '合計' end yf,
     sum(cast(bqbj1 as money)) bjhz,
     sum(cast(bqll1 as money)) lxhz,
     sum(cast(bqwh as money)) whhz
from (select SUBSTRING(rq,6,2) yf,bqbj1,bqll1,bqwh
      from formtable_main_84_dt1 d 
      where rq between '2017-07-01' and '2017-09-30') as a
group by yf
with rollup

多行變多列

SELECT name ,
MAX(CASE type WHEN '數學' THEN score ELSE 0 END ) math,
MAX(CASE type WHEN '英語' THEN score ELSE 0 END ) English ,
MAX(CASE type WHEN '語文' THEN score ELSE 0 END ) Chinese 
FROM stu  
GROUP BY name

reference

[1] https://blog.csdn.net/yu_kang/article/details/78331581
[2] https://blog.csdn.net/rainyspring4540/article/details/50231435

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