Skip to content

Commit f35d580

Browse files
authored
Update monthly-transactions-ii.sql
1 parent 3b86cd7 commit f35d580

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

MySQL/monthly-transactions-ii.sql

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
# Time: O(n)
22
# Space: O(n)
33

4-
select month, country,
5-
sum(IF( type = "approved", 1, 0)) as approved_count,
6-
sum(IF( type = "approved", amount, 0)) as approved_amount,
7-
sum(IF( type = "chargeback", 1, 0)) as chargeback_count,
8-
sum(IF( type = "chargeback", amount, 0)) as chargeback_amount
9-
from (
10-
(
11-
select left(t.trans_date, 7) as month, t.country, amount,'approved' as type
12-
from Transactions as t
13-
where state='approved'
14-
)
15-
union all (
16-
select left(c.trans_date, 7) as month, t.country, amount,'chargeback' as type
17-
from Transactions as t inner join Chargebacks as c
18-
on t.id = c.trans_id
19-
)
20-
) as tt
21-
group by tt.month, tt.country
4+
SELECT MONTH,
5+
country,
6+
sum(IF(TYPE = "approved", 1, 0)) AS approved_count,
7+
sum(IF(TYPE = "approved", amount, 0)) AS approved_amount,
8+
sum(IF(TYPE = "chargeback", 1, 0)) AS chargeback_count,
9+
sum(IF(TYPE = "chargeback", amount, 0)) AS chargeback_amount
10+
FROM (
11+
(SELECT left(t.trans_date, 7) AS MONTH,
12+
t.country,
13+
amount,
14+
'approved' AS TYPE
15+
FROM Transactions AS t
16+
WHERE state='approved' )
17+
UNION ALL
18+
(SELECT left(c.trans_date, 7) AS MONTH,
19+
t.country,
20+
amount,
21+
'chargeback' AS TYPE
22+
FROM Transactions AS t
23+
INNER JOIN Chargebacks AS c ON t.id = c.trans_id)) AS tt
24+
GROUP BY tt.month,
25+
tt.country;

0 commit comments

Comments
 (0)