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 Name | Type | Description |
|---|---|---|
id | int | Unique transaction identifier |
country | varchar | Country where transaction occurred |
state | enum | Transaction status: "approved" or "declined" |
amount | int | Transaction amount in cents |
trans_date | date | Date 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
ā Linear Growth
Space Complexity
O(k)
Space for k unique month-country combinations in result set
ā 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
š”
Explanation
AI Ready
š” Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code