结果增加一行合计
示例代码:
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.youkuaiyun.com/yu_kang/article/details/78331581
[2] https://blog.youkuaiyun.com/rainyspring4540/article/details/50231435