请各位指教一下,
该如何查询出每个公司不同类型不同时间的合计值
最好是一条sql,多条也可以
select
name,
type,
sum(case when y = 2020 then total else 0 end) as '前年总计',
sum(case when y = 2020 or y = 2021 or y = 2022 then total else 0 end) as '前年1-本月总计',
sum(case when y = 2021 then total else 0 end) as '去年总计',
sum(case when y = 2021 or y = 2022 then total else 0 end) as '去年1-本月总计',
sum(case when y = 2022 then total else 0 end) as '今年1-本月总计'
from (
select name, type, year(date) as y,sum(value) as total from t group by name, type, y
) t
group by name,type