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

猜你喜欢

转载自blog.csdn.net/qq_19672707/article/details/100132066