【问题标题】:Filtering values and sum in Mysql [closed]在Mysql中过滤值和总和[关闭]
【发布时间】:2021-04-05 13:29:46
【问题描述】:

嗨,请帮助我解决以下问题。对此非常不满!

我有 3 列代理商、金额和年份

我想按代理商对数据进行分组并对这些值求和

例如:

enter image description here

我需要这样的答案

enter image description here

【问题讨论】:

  • 考虑处理应用代码中数据显示的问题

标签: mysql sql sum pivot aggregate-functions


【解决方案1】:

使用条件聚合:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 2016-06-26
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 2020-02-25
    相关资源
    最近更新 更多