You have two tables: Transactions and Chargebacks.
The Transactions table contains information about incoming transactions with columns for id, country, state (approved/declined), amount, and trans_date.
The Chargebacks table contains information about chargebacks with trans_id (foreign key to Transactions.id) and trans_date (chargeback date).
Task: For each month and country, find:
- Number of approved transactions and their total amount
- Number of chargebacks and their total amount
Note: Ignore rows with all zeros. Chargebacks can occur for any transaction (approved or declined).
Table Schema
| Column Name | Type | Description |
|---|---|---|
id
PK
|
int | Primary key, unique transaction ID |
country
|
varchar | Country where transaction occurred |
state
|
enum | Transaction status: 'approved' or 'declined' |
amount
|
int | Transaction amount |
trans_date
|
date | Transaction date |
| Column Name | Type | Description |
|---|---|---|
trans_id
PK
|
int | Foreign key referencing Transactions.id |
trans_date
|
date | Chargeback date |
Input & Output
| id | country | state | amount | trans_date |
|---|---|---|---|---|
| 1 | US | approved | 1000 | 2019-05-18 |
| 2 | US | declined | 2000 | 2019-06-17 |
| 3 | US | approved | 3000 | 2019-06-17 |
| 4 | US | approved | 4000 | 2019-06-17 |
| trans_id | trans_date |
|---|---|
| 2 | 2019-06-15 |
| 3 | 2019-06-15 |
| month | country | approved_count | approved_amount | chargeback_count | chargeback_amount |
|---|---|---|---|---|---|
| 2019-05 | US | 1 | 1000 | 0 | 0 |
| 2019-06 | US | 2 | 7000 | 2 | 5000 |
For 2019-05: 1 approved transaction (id=1) worth $1000, no chargebacks. For 2019-06: 2 approved transactions (id=3,4) worth $7000 total, and 2 chargebacks (for transactions 2 and 3) worth $5000 total. Note that transaction 2 was declined but still had a chargeback.
| id | country | state | amount | trans_date |
|---|---|---|---|---|
| 1 | US | approved | 1000 | 2019-05-18 |
| 2 | CA | approved | 2000 | 2019-05-18 |
| 3 | CA | declined | 1500 | 2019-05-19 |
| trans_id | trans_date |
|---|---|
| 3 | 2019-06-01 |
| month | country | approved_count | approved_amount | chargeback_count | chargeback_amount |
|---|---|---|---|---|---|
| 2019-05 | CA | 1 | 2000 | 0 | 0 |
| 2019-05 | US | 1 | 1000 | 0 | 0 |
| 2019-06 | CA | 0 | 0 | 1 | 1500 |
Shows data for multiple countries. In May, US had 1 approved transaction ($1000) and CA had 1 approved transaction ($2000). In June, CA had a chargeback for the declined transaction 3 ($1500).
Constraints
-
1 ≤ id ≤ 1000 -
countryis a valid country code -
stateis either'approved'or'declined' -
amount > 0 -
trans_dateis a valid date