Monthly Transactions II - Problem
You're working as a data analyst for a global payment processing company that needs to generate comprehensive monthly financial reports. Your task is to analyze transaction data alongside chargeback information to provide insights into business performance.
You have access to two critical datasets:
- Transactions table: Contains all payment transactions with their approval status, amounts, countries, and dates
- Chargebacks table: Contains dispute information where customers have contested previous transactions
Your goal is to create a monthly summary report that shows, for each month-country combination:
- Number of approved transactions and their total amount
- Number of chargebacks and their total disputed amount
Important notes:
- Chargebacks can occur for any transaction (approved or declined)
- The chargeback date determines which month it belongs to (not the original transaction date)
- Exclude rows where all metrics are zero
- Group results by
YYYY-MMformat and country
Input & Output
example_1.sql โ Basic Example
$
Input:
Transactions table:
+----+---------+---------+--------+------------+
| id | country | state | amount | trans_date |
+----+---------+---------+--------+------------+
| 121| US | approved| 1000 | 2018-12-18 |
| 122| US | declined| 2000 | 2018-12-19 |
| 123| US | approved| 2000 | 2019-01-01 |
| 124| DE | approved| 2000 | 2019-01-07 |
+----+---------+---------+--------+------------+
Chargebacks table:
+----------+------------+
| trans_id | trans_date |
+----------+------------+
| 121 | 2019-01-05 |
| 122 | 2019-01-06 |
+----------+------------+
โบ
Output:
+-------+---------+----------------+-----------------+-----------------+------------------+
| month | country | approved_count | approved_amount | chargeback_count| chargeback_amount|
+-------+---------+----------------+-----------------+-----------------+------------------+
| 2018-12| US | 1 | 1000 | 0 | 0 |
| 2019-01| DE | 1 | 2000 | 0 | 0 |
| 2019-01| US | 1 | 2000 | 2 | 3000 |
+-------+---------+----------------+-----------------+-----------------+------------------+
๐ก Note:
In December 2018, US had 1 approved transaction. In January 2019, US had 1 approved transaction plus 2 chargebacks (one from approved tx 121, one from declined tx 122). Germany had 1 approved transaction in January with no chargebacks.
example_2.sql โ Same Month Scenario
$
Input:
Transactions table:
+----+---------+---------+--------+------------+
| id | country | state | amount | trans_date |
+----+---------+---------+--------+------------+
| 101| CA | approved| 500 | 2019-02-15 |
| 102| CA | approved| 800 | 2019-02-20 |
+----+---------+---------+--------+------------+
Chargebacks table:
+----------+------------+
| trans_id | trans_date |
+----------+------------+
| 101 | 2019-02-25 |
+----------+------------+
โบ
Output:
+-------+---------+----------------+-----------------+-----------------+------------------+
| month | country | approved_count | approved_amount | chargeback_count| chargeback_amount|
+-------+---------+----------------+-----------------+-----------------+------------------+
| 2019-02| CA | 2 | 1300 | 1 | 500 |
+-------+---------+----------------+-----------------+-----------------+------------------+
๐ก Note:
Both the approved transactions and the chargeback happened in February 2019, so they're aggregated together in the same month-country row.
example_3.sql โ Edge Case: Only Chargebacks
$
Input:
Transactions table:
+----+---------+---------+--------+------------+
| id | country | state | amount | trans_date |
+----+---------+---------+--------+------------+
| 201| FR | declined| 1500 | 2018-11-10 |
| 202| FR | declined| 900 | 2018-11-15 |
+----+---------+---------+--------+------------+
Chargebacks table:
+----------+------------+
| trans_id | trans_date |
+----------+------------+
| 201 | 2019-03-10 |
| 202 | 2019-03-12 |
+----------+------------+
โบ
Output:
+-------+---------+----------------+-----------------+-----------------+------------------+
| month | country | approved_count | approved_amount | chargeback_count| chargeback_amount|
+-------+---------+----------------+-----------------+-----------------+------------------+
| 2019-03| FR | 0 | 0 | 2 | 2400 |
+-------+---------+----------------+-----------------+-----------------+------------------+
๐ก Note:
France had no approved transactions in March 2019, but had 2 chargebacks totaling 2400. Since there's activity (chargeback_count > 0), this row is included in results.
Visualization
Tap to expand
Understanding the Visualization
1
Capture Events
Every approved transaction and chargeback is an event with date, country, and amount
2
Group by Time & Location
Aggregate events by month-country combinations using conditional counting
3
Generate Report
Output comprehensive monthly statements showing both positive and negative cash flows
Key Takeaway
๐ฏ Key Insight: The optimal approach treats approved transactions and chargebacks as separate event streams that are unified through conditional aggregation, allowing us to generate comprehensive monthly financial reports efficiently.
Time & Space Complexity
Time Complexity
O(n + m)
Single pass through transactions (n) and chargebacks (m) with efficient grouping
โ Linear Growth
Space Complexity
O(k)
Space for result set where k is number of unique month-country combinations
โ Linear Space
Constraints
- 1 โค transactions.length โค 104
- 1 โค chargebacks.length โค 103
- 1 โค id, trans_id, amount โค 106
- state is either 'approved' or 'declined'
- country is a valid 2-letter country code
- All dates are in YYYY-MM-DD format
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code