-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmonthly-transactions-ii.sql
159 lines (145 loc) · 5.81 KB
/
monthly-transactions-ii.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Table: Transactions
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| id | int |
| country | varchar |
| state | enum |
| amount | int |
| trans_date | date |
+----------------+---------+
id is the primary key of this table.
The table has information about incoming transactions.
The state column is an enum of type ["approved", "declined"].
Table: Chargebacks
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| trans_id | int |
| charge_date | date |
+----------------+---------+
Chargebacks contains basic information regarding incoming chargebacks from some transactions placed in Transactions table.
trans_id is a foreign key to the id column of Transactions table.
Each chargeback corresponds to a transaction made previously even if they were not approved.
Write an SQL query 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 query, given the month and country, ignore rows with all zeros.
The query result format is in the following example:
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 | approved | 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 |
+------------+------------+
Result table:
+----------+---------+----------------+-----------------+-------------------+--------------------+
| 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 |
+----------+---------+----------------+-----------------+-------------------+--------------------+
*/
# V0
# idea : union all
select month, country,
sum(case when type='approved' then 1 else 0 end) as approved_count,
sum(case when type='approved' then amount else 0 end) as approved_amount,
sum(case when type='chargeback' then 1 else 0 end) as chargeback_count,
sum(case when type='chargeback' then amount else 0 end) as chargeback_amount
from (
(
select left(t.trans_date, 7) as month, t.country, amount,'approved' as type
from Transactions as t
where state='approved'
)
union all (
select left(c.trans_date, 7) as month, t.country, amount,'chargeback' as type
from Transactions as t join Chargebacks as c
on t.id = c.trans_id
)
) as tt
group by tt.month, tt.country
# V1
# idea : union all
# https://code.dennyzhang.com/monthly-transactions-ii
select month, country,
sum(case when type='approved' then 1 else 0 end) as approved_count,
sum(case when type='approved' then amount else 0 end) as approved_amount,
sum(case when type='chargeback' then 1 else 0 end) as chargeback_count,
sum(case when type='chargeback' then amount else 0 end) as chargeback_amount
from (
(
select left(t.trans_date, 7) as month, t.country, amount,'approved' as type
from Transactions as t
where state='approved'
)
union all (
select left(c.trans_date, 7) as month, t.country, amount,'chargeback' as type
from Transactions as t join Chargebacks as c
on t.id = c.trans_id
)
) as tt
group by tt.month, tt.country
# V1'
# https://code.dennyzhang.com/monthly-transactions-ii
select month, country,
sum(case when type='approved' then count else 0 end) as approved_count,
sum(case when type='approved' then amount else 0 end) as approved_amount,
sum(case when type='chargeback' then count else 0 end) as chargeback_count,
sum(case when type='chargeback' then amount else 0 end) as chargeback_amount
from (
(
select left(t.trans_date, 7) as month, t.country,
count(1) as count, sum(amount) as amount,'approved' as type
from Transactions as t left join Chargebacks as c
on t.id = c.trans_id
where state='approved'
group by left(t.trans_date, 7), t.country
)
union (
select left(c.trans_date, 7) as month, t.country,
count(1) as count, sum(amount) as amount,'chargeback' as type
from Transactions as t join Chargebacks as c
on t.id = c.trans_id
group by left(c.trans_date, 7), t.country
)
) as tt
group by tt.month, tt.country
# V2
# Time: O(n)
# Space: O(n)
SELECT month,
country,
SUM(IF(type = 'approved', 1, 0)) AS approved_count,
SUM(IF(type = 'approved', amount, 0)) AS approved_amount,
SUM(IF(type = 'chargeback', 1, 0)) AS chargeback_count,
SUM(IF(type = 'chargeback', amount, 0)) AS chargeback_amount
FROM (
(SELECT LEFT(t.trans_date, 7) AS month,
t.country,
amount,
'approved' AS type
FROM Transactions AS t
WHERE state = 'approved' )
UNION ALL
(SELECT LEFT(c.trans_date, 7) AS month,
t.country,
amount,
'chargeback' AS type
FROM Transactions AS t
INNER JOIN Chargebacks AS c ON t.id = c.trans_id)) AS tt
GROUP BY tt.month,
tt.country;