输出水平聚合列值的结果

nkoocmlb  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(266)

我有个问题:

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 类型-所以我不必担心“布拉布拉赫”是一种身份类型。

fquxozlt

fquxozlt1#

您可以尝试使用条件聚合函数,如果您使用 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`

测试DLL

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`

结果:

|  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 |

相关问题