Monthly Transactions I - Problem

šŸ“Š Monthly Transaction Report Generator

You're working for a fintech company that processes millions of transactions worldwide. Your task is to create a monthly financial report that summarizes transaction data by country and month.

Given a Transactions table with the following structure:

Column NameTypeDescription
idintUnique transaction identifier
countryvarcharCountry where transaction occurred
stateenumTransaction status: "approved" or "declined"
amountintTransaction amount in cents
trans_datedateDate when transaction was processed

Your mission: Write an SQL query to generate a comprehensive monthly report showing:

  • šŸ“… Month and Country breakdown
  • šŸ“ˆ Total transactions count and total amount
  • āœ… Approved transactions count and approved amount

The report should help executives quickly understand transaction patterns and approval rates across different markets.

Input & Output

example_1.sql — Basic Monthly Report
$ 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|
› Output: | month | country | trans_count | trans_total_amount | approved_count | approved_total_amount | |---------|---------|-------------|-------------------|----------------|-----------------------| | 2018-12 | US | 2 | 3000 | 1 | 1000 | | 2019-01 | DE | 1 | 2000 | 1 | 2000 | | 2019-01 | US | 1 | 2000 | 1 | 2000 |
šŸ’” Note: December 2018: US had 2 transactions totaling $3000, with 1 approved for $1000. January 2019: DE had 1 approved transaction for $2000, US had 1 approved transaction for $2000.
example_2.sql — All Declined Transactions
$ Input: Transactions table: | id | country | state | amount | trans_date | |----|---------|----------|--------|-----------| | 201| CA | declined | 500 | 2019-03-15| | 202| CA | declined | 750 | 2019-03-20| | 203| MX | declined | 1200 | 2019-03-15|
› Output: | month | country | trans_count | trans_total_amount | approved_count | approved_total_amount | |---------|---------|-------------|-------------------|----------------|-----------------------| | 2019-03 | CA | 2 | 1250 | 0 | 0 | | 2019-03 | MX | 1 | 1200 | 0 | 0 |
šŸ’” Note: March 2019: Canada had 2 declined transactions totaling $1250 with 0 approved. Mexico had 1 declined transaction for $1200 with 0 approved.
example_3.sql — Mixed Approval Rates
$ Input: Transactions table: | id | country | state | amount | trans_date | |----|---------|----------|--------|-----------| | 301| UK | approved | 800 | 2020-06-10| | 302| UK | approved | 1200 | 2020-06-15| | 303| UK | declined | 600 | 2020-06-20| | 304| FR | approved | 900 | 2020-06-25|
› Output: | month | country | trans_count | trans_total_amount | approved_count | approved_total_amount | |---------|---------|-------------|-------------------|----------------|-----------------------| | 2020-06 | FR | 1 | 900 | 1 | 900 | | 2020-06 | UK | 3 | 2600 | 2 | 2000 |
šŸ’” Note: June 2020: France had 1 transaction (100% approved) for $900. UK had 3 transactions totaling $2600, with 2 approved totaling $2000 (67% approval rate).

Time & Space Complexity

Time Complexity
ā±ļø
O(n)

Single pass through all transactions with efficient grouping

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

Space for k unique month-country combinations in result set

n
2n
āœ“ Linear Space

Constraints

  • 1 ≤ number of transactions ≤ 105
  • Amount values are integers (can be negative for refunds)
  • State is enum: ['approved', 'declined']
  • Date format in result should be 'YYYY-MM'
  • Country names are non-empty varchar strings
Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 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