高级农民
- 积分
- 4176
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-1-22
- 最后登录
- 1970-1-1
|
1205. Monthly Transactions II
Medium
Topics
SQL Schema
Pandas Schema
Table: Transactions
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| id | int |
| country | varchar |
| state | enum |
| amount | int |
| trans_date | date |
+----------------+---------+
id is the column of unique values of this table.
The table has information about incoming transactions.
The state column is an ENUM (category) of type ["approved", "declined"].
Table: Chargebacks
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| trans_id | int |
| trans_date | date |
+----------------+---------+
Chargebacks contains basic information regarding incoming chargebacks from some transactions placed in Transactions table.
trans_id is a foreign key (reference column) to the id column of Transactions table.
Each chargeback corresponds to a transaction made previously even if they were not approved.
Write a solution to find for each month and country: the number of approved transactions and their total amount, the number of chargebacks, and their total amount.
Note: In your solution, given the month and country, ignore rows with all zeros.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
Transactions table:
+-----+---------+----------+--------+------------+
| 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 table:
+----------+------------+
| trans_id | trans_date |
+----------+------------+
| 102 | 2019-05-29 |
| 101 | 2019-06-30 |
| 105 | 2019-09-18 |
+----------+------------+
Output:
+---------+---------+----------------+-----------------+------------------+-------------------+
| 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 |
+---------+---------+----------------+-----------------+------------------+-------------------+
思路
这题的难点是:批准交易和退单属于不同的月份,而且可能出现在不同的月份、国家组合里。比如示例里 2019-09 只有退单,没有批准交易,但仍然要输出。
最简单的做法是把两种事件都变成统一格式的行,再一起汇总:
批准交易:按交易自己的日期取月份,approved_count = 1,approved_amount = amount,退单两列为 0。
退单:按退单日期取月份,国家和金额要回到 Transactions 表里查,chargeback_count = 1,chargeback_amount = amount,批准两列为 0。
两部分用 UNION ALL 合并,再按 (month, country) 分组求和。
这样做的好处是,只有真正发生过事件的 (month, country) 才会有行,所以"忽略全 0 的行"自然满足,也不需要 FULL OUTER JOIN(MySQL 不支持)。- # Write your MySQL query statement below
- WITH events AS (
- SELECT
- DATE_FORMAT(trans_date, '%Y-%m') AS month,
- country,
- 1 AS approved_count,
- amount AS approved_amount,
- 0 AS chargeback_count,
- 0 AS chargeback_amount
- FROM Transactions
- WHERE state = 'approved'
- UNION ALL
- SELECT
- DATE_FORMAT(c.trans_date, '%Y-%m') AS month,
- t.country,
- 0, 0, 1, t.amount
- FROM Chargebacks c
- JOIN Transactions t ON c.trans_id = t.id
- )
- SELECT
- month,
- country,
- SUM(approved_count) AS approved_count,
- SUM(approved_amount) AS approved_amount,
- SUM(chargeback_count) AS chargeback_count,
- SUM(chargeback_amount) AS chargeback_amount
- FROM events
- GROUP BY month, country;
复制代码 |
|