题目描述

(通过次数8,184 | 提交次数17,843,通过率45.87%)

Transactions 记录表
+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| id             | int     |
| country        | varchar |
| state          | enum    |
| amount         | int     |
| trans_date     | date    |
+----------------+---------+
id 是这个表的主键。
该表包含有关传入事务的信息。
状态列是类型为 [approved(已批准)、declined(已拒绝)] 的枚举。
Chargebacks 表
+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| trans_id       | int     |
| trans_date     | date    |
+----------------+---------+
退单包含有关放置在事务表中的某些事务的传入退单的基本信息。
trans_id 是 transactions 表的 id 列的外键。
每项退单都对应于之前进行的交易,即使未经批准。

编写一个 SQL查询,以查找每个月和每个国家/地区的信息:已批准交易的数量及其总金额、退单的数量及其总金额。
注意:在您的查询中,只需显示给定月份和国家,忽略所有为零的行。
以 任意顺序 返回结果表。

查询结果格式如下所示。
示例 1:
输入:
Transactions 表:
+-----+---------+----------+--------+------------+
| id  | country | state    | amount | trans_date |
+-----+---------+----------+--------+------------+
| 101 | US      | approved | 1000   | 2019-05-18 |
| 102 | US      | declined | 2000   | 2019-05-19 |
| 103 | US      | approved | 3000   | 2019-06-10 |
| 104 | US      | declined | 4000   | 2019-06-13 |
| 105 | US      | approved | 5000   | 2019-06-15 |
+-----+---------+----------+--------+------------+
Chargebacks 表:
+----------+------------+
| trans_id | trans_date |
+----------+------------+
| 102      | 2019-05-29 |
| 101      | 2019-06-30 |
| 105      | 2019-09-18 |
+----------+------------+
输出:
+---------+---------+----------------+-----------------+------------------+-------------------+
| month   | country | approved_count | approved_amount | chargeback_count | chargeback_amount |
+---------+---------+----------------+-----------------+------------------+-------------------+
| 2019-05 | US      | 1              | 1000            | 1                | 2000              |
| 2019-06 | US      | 2              | 8000            | 1                | 1000              |
| 2019-09 | US      | 0              | 0               | 1                | 5000              |
+---------+---------+----------------+-----------------+------------------+-------------------+

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/monthly-transactions-ii
//测试数据
Create table If Not Exists Transactions (id int, country varchar(4), state enum('approved', 'declined'), amount int, trans_date date);
Create table If Not Exists Chargebacks (trans_id int, trans_date date);

insert into Transactions (id, country, state, amount, trans_date) values ('101', 'US', 'approved', '1000', '2019-05-18');
insert into Transactions (id, country, state, amount, trans_date) values ('102', 'US', 'declined', '2000', '2019-05-19');
insert into Transactions (id, country, state, amount, trans_date) values ('103', 'US', 'approved', '3000', '2019-06-10');
insert into Transactions (id, country, state, amount, trans_date) values ('104', 'US', 'declined', '4000', '2019-06-13');
insert into Transactions (id, country, state, amount, trans_date) values ('105', 'US', 'approved', '5000', '2019-06-15');

insert into Chargebacks (trans_id, trans_date) values ('102', '2019-05-29');
insert into Chargebacks (trans_id, trans_date) values ('101', '2019-06-30');
insert into Chargebacks (trans_id, trans_date) values ('105', '2019-09-18');

解题思路

根据题目要求,按月份和交易状态统计交易金额即可。
但题目有一个要求:对于那些在交易表中为approved状态的交易,如果实际上已经被退单(在Chargebacks表中出现),那么这笔交易既要统计在approved中,又要统计在chargeback中。
所以,一条交易,有可能会发散为两种状态后再进行汇总统计。那么可以分别查出两种状态的子集,然后合并汇总即可。

参考SQL

未特别说明的情况下,参考SQL为基于MySQL8.0实现。
select
    trans_date,
    country,
    state,
    amount
from Transactions
where state = 'approved'
union all
select
    a.trans_date,
    b.country,
    'declined' state,
    b.amount
from Chargebacks a
inner join Transactions b
on a.trans_id = b.id
)
select
    date_format(trans_date,'%Y-%m') month,
    country,
    count(case when state = 'approved' then 1 else null end) approved_count,
    sum(case when state = 'approved' then amount else 0 end) approved_amount,
    count(case when state = 'declined' then 1 else null end) chargeback_count,
    sum(case when state = 'declined' then amount else 0 end) chargeback_amount
from tmp
group by date_format(trans_date,'%Y-%m'),country;

picture loss