水平聚合列值的输出结果

Output results aggregating column values horizontally

我有这个查询:

SELECT
count(*), `merchant_id`, `merchant_finance_id`, `merchant_channel_id`, `status`
FROM `application` 
WHERE `created_at` >= '2018-04-30' AND `created_at` < '2018-05-01'
GROUP BY `merchant_id`, `merchant_finance_id`, `merchant_channel_id`, `status`

结果如下:

-------------------------------------------------------------------------------
| count(*) | merchant_id | merchant_finance_id | merchant_channel_id | status |
-------------------------------------------------------------------------------
| 2 | D8E2459CD78C | 55E4D520AC1C | 66A1861918C5 | ACCEPTED                   |
| 2 | D8E2459CD78C | 55E4D520AC1C | 66A1861918C5 | REFERRED                   |
| 1 | E50E50212627 | 6F8F15729DA7 | E02ACD64B452 | AWAITING-ACTIVATION        |
| 2 | E50E50212627 | 6F8F15729DA7 | E02ACD64B452 | DEPOSIT-PAID               |
| 1 | E50E50212627 | 6F8F15729DA7 | E02ACD64B452 | READY                      |
| 1 | E50E50212627 | 6F8F15729DA7 | E02ACD64B452 | REFERRED                   |
| 1 | F04FBD7AEB30 | C11CBD2FC1F8 | 21F2D435EA9D | AWAITING-ACTIVATION        |
-------------------------------------------------------------------------------

但是有没有办法将 status 排列到子栏中呢?例如:

-------------------------------------------------------------------------------
| count(*) | merchant_id | merchant_finance_id | merchant_channel_id | accepted | referred | awaiting_activation | deposit_paid | ready
-------------------------------------------------------------------------------
| 2 | D8E2459CD78C | 55E4D520AC1C | 66A1861918C5 | 2 | 2 | 0 | 0 | 0 |
| 5 | E50E50212627 | 6F8F15729DA7 | E02ACD64B452 | 0 | 1 | 1 | 2 | 1 |
| 1 | F04FBD7AEB30 | C11CBD2FC1F8 | 21F2D435EA9D | 0 | 0 | 1 | 0 | 0 |
-------------------------------------------------------------------------------

注:

有一个不同 status 类型的定义列表 - 所以我不必担心 "BLAHBLAH" 不知何故成为状态类型。

你可以尝试使用条件聚合函数,如果你使用Mysql你可以这样做最简单的。

SELECT
    count(*), 
    `merchant_id`,
    `merchant_finance_id`, 
    `merchant_channel_id`, 
    SUM(`status`='ACCEPTED'),
    SUM(`status`='REFERRED'),
    SUM(`status`='awaiting_activation'),
    SUM(`status`='deposit_paid'),
    SUM(`status`='READY')
FROM `application` 
WHERE `created_at` >= '2018-04-30' AND `created_at` < '2018-05-01'
GROUP BY `merchant_id`, `merchant_finance_id`, `merchant_channel_id`

TestDLL

CREATE TABLE T(
   merchant_id varchar(50),
   merchant_finance_id varchar(50),
   merchant_channel_id varchar(50),
   `status` varchar(50)
);



INSERT INTO T VALUES ('D8E2459CD78C', '55E4D520AC1C','66A1861918C5' ,'ACCEPTED');           
INSERT INTO T VALUES ('D8E2459CD78C', '55E4D520AC1C','66A1861918C5' ,'REFERRED');           
INSERT INTO T VALUES ('E50E50212627', '6F8F15729DA7','E02ACD64B452' ,'AWAITING-ACTIVATION');
INSERT INTO T VALUES ('E50E50212627', '6F8F15729DA7','E02ACD64B452' ,'DEPOSIT-PAID');       
INSERT INTO T VALUES ('E50E50212627', '6F8F15729DA7','E02ACD64B452' ,'READY');             
INSERT INTO T VALUES ('E50E50212627', '6F8F15729DA7','E02ACD64B452' ,'REFERRED');           
INSERT INTO T VALUES ('F04FBD7AEB30', 'C11CBD2FC1F8','21F2D435EA9D' ,'AWAITING-ACTIVATION');

查询 1:

SELECT
    `merchant_id`,
    `merchant_finance_id`, 
    `merchant_channel_id`, 
    SUM(`status`='accepted') accepted,
    SUM(`status`='referred') referred,
    SUM(`status`='awaiting_activation') awaiting_activation,
    SUM(`status`='deposit_paid') deposit_paid,
    SUM(`status`='ready') ready
FROM T
GROUP BY `merchant_id`, `merchant_finance_id`, `merchant_channel_id`

Results:

|  merchant_id | merchant_finance_id | merchant_channel_id | accepted | referred | awaiting_activation | deposit_paid | ready |
|--------------|---------------------|---------------------|----------|----------|---------------------|--------------|-------|
| D8E2459CD78C |        55E4D520AC1C |        66A1861918C5 |        1 |        1 |                   0 |            0 |     0 |
| E50E50212627 |        6F8F15729DA7 |        E02ACD64B452 |        0 |        1 |                   0 |            0 |     1 |
| F04FBD7AEB30 |        C11CBD2FC1F8 |        21F2D435EA9D |        0 |        0 |                   0 |            0 |     0 |