|
1 | 1 | # Time: O(n)
|
2 | 2 | # Space: O(n)
|
3 | 3 |
|
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