输出结果水平汇总列值

时间:2018-08-06 17:34:18

标签: mysql mysql-5.7

我有这个查询:

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”是一种状态类型。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用条件汇总函数。如果您使用Input,则可以执行以下最简单的操作。

<Select
  input={
    <Input
      classes={myClasses.input}
    />
  }
>
  ...
</Select>

TestDLL

Mysql

查询1

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`

Results

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');