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-MM format 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
ApprovedTransactions๐Ÿ’ฐ Money InChargebacks& Disputes๐Ÿ’ธ Money OutMonthly GroupingBy Country & Date๐Ÿ“… YYYY-MM formatFinancial ReportCount & Sum Both๐Ÿ“Š Net Analysis๐Ÿ’ก Key: Different dates for transactions vs chargebacks!Transaction date determines when approval is countedChargeback date determines when dispute is counted
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

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Space for result set where k is number of unique month-country combinations

n
2n
โœ“ 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
Asked in
Stripe 35 PayPal 28 Square 22 Visa 18 Amazon 15
28.4K Views
High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen