按总价值和总交易次数查找付款方式

时间:2019-01-30 19:04:59

标签: mysql group-by left-join

我有付款表,其中包含以下数据:

模式(MySQL v5.7)

CREATE TABLE payments (
  Transaction_ID INT NOT NULL,
  Business_ID INT NOT NULL,
  Payment_Amount DECIMAL(25,2),
  Payment_Type VARCHAR(6),
  PRIMARY KEY (Transaction_ID)
 );

CREATE TABLE business_results (
  Business_ID INT NOT NULL,
  Top_Payment_Type_by_Dollar_Amount VARCHAR(6),
  Top_Payment_Type_by_Transactions VARCHAR(6),
  PRIMARY KEY (Business_ID)
);

INSERT INTO payments (Transaction_ID, Business_ID, Payment_Amount, Payment_Type)
  VALUES (1,2, 3.00, 'CASH'),
    (2,1,15.00,'CREDIT'),
    (3,4,5.00,'CASH'),
    (4,3,31.00,'CASH'),
    (5,4,2.00,'CREDIT'),
    (6,2,25.00,'CASH'),
    (7,2,30.00,'CREDIT'),
    (8,3,16.00,'CASH'),
    (9,1,7.00,'CREDIT'),
    (10,4,4.00,'CREDIT');

查询#1

select * from payments;

| Transaction_ID | Business_ID | Payment_Amount | Payment_Type |
| -------------- | ----------- | -------------- | ------------ |
| 1              | 2           | 3              | CASH         |
| 2              | 1           | 15             | CREDIT       |
| 3              | 4           | 5              | CASH         |
| 4              | 3           | 31             | CASH         |
| 5              | 4           | 2              | CREDIT       |
| 6              | 2           | 25             | CASH         |
| 7              | 2           | 30             | CREDIT       |
| 8              | 3           | 16             | CASH         |
| 9              | 1           | 7              | CREDIT       |
| 10             | 4           | 4              | CREDIT       |

View on DB Fiddle

交易类型由“贷方”或“现金”组成

我正在尝试创建具有以下结果的查询:

  • 第1列:Business_ID
  • 第2列:按付款方式的总和,总金额最高的付款方式的国家
  • 第3列:付款类型最多的国家/地区

根据提供的数据,结果应为以下内容:


+-------------+-----------------------------------+----------------------------------+
| Business_ID | Top_Payment_Type_by_Dollar_Amount | Top_Payment_Type_by_Transactions |
+-------------+-----------------------------------+----------------------------------+
|           1 | CREDIT                            | CREDIT                           |
|           2 | CREDIT                            | CASH                             |
|           3 | CASH                              | CASH                             |
|           4 | CREDIT                            | CREDIT                           |
+-------------+-----------------------------------+----------------------------------+

单个 MySQL查询中,我将如何进行呢?

-

1 个答案:

答案 0 :(得分:0)

所以,问题的第一部分可以这样解决...

SELECT x.business_id
     , x.payment_type
  FROM payments x 
  JOIN 
     ( SELECT business_id
            , MAX(payment_amount) payment_amount 
         FROM (SELECT * FROM payments) n
        GROUP  
           BY business_id
     ) y 
    ON y.business_id = x.business_id 
   AND y.payment_amount = x.payment_amount
 ORDER
    BY x.business_id;

...子查询 n 并非绝对必要-我可以(并且应该)刚刚使用FROM payments,但是通过这种方式,它为解决下一部分提供了线索问题所在,从本质上讲,您只需将n替换为...

SELECT business_id
     , payment_type
     , SUM(payment_amount) total 
  FROM payments 
 GROUP 
    BY business_id
     , payment_type; 

...并稍微调整主连接子句