Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql中的筛选值和求和_Mysql_Sql_Sum_Pivot_Aggregate Functions - Fatal编程技术网

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

考虑应用程序代码中的数据显示问题