【问题标题】:Group by 2 field in mysql [duplicate]在mysql中按2字段分组[重复]
【发布时间】:2016-11-11 00:37:23
【问题描述】:

如何按 2 列的数据分组,但结果在 1 行中(就像我们加入它时一样)。

这是桌子'jembatan'

id    nama   tahun    jumlah
-----------------------------
1     A      2011     12
2     B      2011     10
3     A      2011     23
4     B      2012     11

我想要这样的结果:

id    totalA     totalB     tahun
---------------------------------
      25         10         2011
      0          11         2012

这样怎么办?

【问题讨论】:

  • 通过基础研究

标签: mysql sql group-by


【解决方案1】:

你想要条件聚合:

select sum(case when nama = 'A' then jumlah else 0 end) as TotalA,
       sum(case when nama = 'B' then jumlah else 0 end) as TotalB,
       tahun
from t
group by tahun;

【讨论】:

    猜你喜欢
    • 2011-04-08
    • 2016-01-27
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多