Transactions record form
+ --------- + ---------------- +
| Column the Name | Type |
+ --------------- - + --------- +
| the above mentioned id | int |
| Country | VARCHAR |
| State | enum |
| AMOUNT | int |
| TRANS_DATE | DATE |
+ ------------ + --------- + ----
the above mentioned id is the primary key of this table.
This table contains information about incoming transactions.
Status column is of type [approved (already approved), declined (Rejected)] enumeration.
Chargebacks 表
+ --------- + ---------------- +
| Column the Name | Type |
+ --------------- - + --------- +
| TRANS_ID | int |
| CHARGE_DATE | DATE |
+ --------- + ---------------- +
chargeback contains basic information about incoming chargeback certain matters be placed in the transaction table.
trans_id foreign key table id column transactions.
Each corresponds to a chargeback transactions made before, even without approval.
Write a SQL query to find each month and each country / region has approved the number and total amount of the transaction, the number and total amount of the chargeback.
Note: in your query, for a given month and the state, ignoring all the lines to zero.
Results shown in the following format:
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 | approved | 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 |
+------------+------------+
Result 表:
+----------+---------+----------------+-----------------+-------------------+--------------------+
| month | country | approved_count | approved_amount | chargeback_count | chargeback_amount |
+----------+---------+----------------+-----------------+-------------------+--------------------+
| 2019-05 | US | 1 | 1000 | 1 | 2000 |
| 2019-06 | US | 3 | 12000 | 1 | 1000 |
| 2019-09 | US | 0 | 0 | 1 | 5000 |
+----------+---------+----------------+-----------------+-------------------+--------------------+
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/monthly-transactions-ii
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Moderation: to write a SQL query to find each month and each country / region has approved the number and total amount of the transaction, the number and total amount of the chargeback.
Every month, each country, the approval number and the total number of orders and total withdrawal.
Thinking: the number of queries state approval, if approval is. Packet summation problem. Chargeback number of single-table queries need to retreat, and the number of packets belonging to a single table in the back.
Problem solving:
Union: the results of the two sets and set operations, not including duplicates, while default sorting rule;
union All: two result sets and set operations, including duplicates, do not sort;
This question is the idea that
the number of first detected each month and each country / region and the total amount of the transaction has been approved
select country,state,amount,date_format(t.trans_date,'%Y-%m') as month,0 as tag
from Transactions t where state!='declined'
-- 练习
select county,state,amount,date_format(t.trans_date,'%Y-%m') as month,0 as tag
from Transactions t where state! = 'declined';
And then find out the number of chargebacks per month and each country / region and the total amount
select country,state,amount,date_format(c.trans_date,'%Y-%m') as month,1 as tag
from Transactions t
right join Chargebacks c on t.id=c.trans_id
-- 退单的数量及总额
on t.id = c.trans_id
Merger, alone about the association is not complete because it requires Chargebacks inside trans_date should be included in,
when the attention of the merger with Union this will not overwrite duplicate rows, determine whether duplicate rows is the national average amount of years this is consistent prone
Statistics last count, sum sum, packet ordering
Here with the tag to distinguish between transactions or chargebacks, statistical convenient after merger
select month,
country,
count(case when state='approved' and tag=0 then 1 else null end ) as approved_count,
sum(case when state='approved' and tag=0 then amount else 0 end ) as approved_amount,
count(case when tag=1 then 1 else null end ) as chargeback_count,
sum(case when tag=1 then amount else 0 end ) as chargeback_amount
from(
select country,state,amount,date_format(c.trans_date,'%Y-%m') as month,1 as tag
from Transactions t
right join Chargebacks c on t.id=c.trans_id
union all
select country,state,amount,date_format(t.trans_date,'%Y-%m') as month,0 as tag
from Transactions t where state!='declined'
) a group by country,month order by month,country
Method Two:
-- Write your MySQL query statement below
SELECT date_format(a.trans_date, '%Y-%m') AS month, a.country
, COUNT(CASE
WHEN state = 'approved' THEN 1
ELSE NULL
END) AS approved_count, SUM(CASE
WHEN state = 'approved' THEN amount
ELSE 0
END) AS approved_amount
, COUNT(CASE
WHEN state IS NULL THEN 1
ELSE NULL
END) AS chargeback_count, SUM(CASE
WHEN state IS NULL THEN amount
ELSE 0
END) AS chargeback_amount
FROM (
SELECT *
FROM Transactions
UNION ALL
SELECT a.trans_id AS id, b.country, NULL, b.amount, a.trans_date
FROM Chargebacks a
JOIN Transactions b ON a.trans_id = b.id
) a
WHERE a.state != 'declined' or a.state is null
GROUP BY a.country, date_format(a.trans_date, '%Y-%m')
Knowledge points: