You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- The table has information about incoming transactions.
15
+
-- The state column is an enum of type ["approved", "declined"].
16
+
-- Table: Chargebacks
17
+
18
+
-- +----------------+---------+
19
+
-- | Column Name | Type |
20
+
-- +----------------+---------+
21
+
-- | trans_id | int |
22
+
-- | charge_date | date |
23
+
-- +----------------+---------+
24
+
-- Chargebacks contains basic information regarding incoming chargebacks from some transactions placed in Transactions table.
25
+
-- trans_id is a foreign key to the id column of Transactions table.
26
+
-- Each chargeback corresponds to a transaction made previously even if they were not approved.
27
+
28
+
29
+
-- 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.
30
+
31
+
-- Note: In your query, given the month and country, ignore rows with all zeros.
32
+
33
+
-- The query result format is in the following example:
(select country, extract('month'from trans_date), state, count(*) as approved_count, sum(amount) as approved_amount
67
+
from transactions
68
+
where state ='approved'
69
+
group by1, 2, 3),
70
+
t2 as(
71
+
selectt.country, extract('month'fromc.trans_date), sum(amount) as chargeback_amount, count(*) as chargeback_count
72
+
from chargebacks c left join transactions t
73
+
on trans_id = id
74
+
group byt.country, extract('month'fromc.trans_date)),
75
+
76
+
t3 as(
77
+
selectt2.date_part, t2.country, coalesce(approved_count,0) as approved_count, coalesce(approved_amount,0) as approved_amount, coalesce(chargeback_count,0) as chargeback_count, coalesce(chargeback_amount,0) as chargeback_amount
selectt1.date_part, t1.country, coalesce(approved_count,0) as approved_count, coalesce(approved_amount,0) as approved_amount, coalesce(chargeback_count,0) as chargeback_count, coalesce(chargeback_amount,0) as chargeback_amount
0 commit comments