Mysql中的筛选值和求和
嗨,请帮我回答下面的问题。对此完全不高兴 我有三个栏目:机构、金额和年份 我想按机构对数据进行分组,并对这些值求和 例如: 我需要这样的答案Mysql中的筛选值和求和,mysql,sql,sum,pivot,aggregate-functions,Mysql,Sql,Sum,Pivot,Aggregate Functions,嗨,请帮我回答下面的问题。对此完全不高兴 我有三个栏目:机构、金额和年份 我想按机构对数据进行分组,并对这些值求和 例如: 我需要这样的答案 使用条件聚合: select agency, sum(case when year = 2018 then amount else 0 end) as amount_2018, sum(case when year = 2019 then amount else 0 end) as amount_2019 from mytable whe
使用条件聚合:
select agency,
sum(case when year = 2018 then amount else 0 end) as amount_2018,
sum(case when year = 2019 then amount else 0 end) as amount_2019
from mytable
where year in (2018, 2019)
group by agency
考虑应用程序代码中的数据显示问题