Odd and Even Transactions - Problem
You are analyzing financial data for a trading platform and need to generate daily reports that categorize transactions by their amounts.
Given a transactions table with transaction IDs, amounts, and dates, your task is to calculate the daily sum of odd-amount transactions and daily sum of even-amount transactions.
Key Requirements:
- Group transactions by date
- Separate odd amounts (1, 3, 5, 101, etc.) from even amounts (2, 4, 6, 100, etc.)
- If no odd transactions exist for a date, show 0 for odd sum
- If no even transactions exist for a date, show 0 for even sum
- Sort results by transaction date in ascending order
This type of analysis helps financial teams understand daily transaction patterns and identify trends in customer behavior.
Input & Output
example_1.sql โ Basic Case
$
Input:
transactions table:
| transaction_id | amount | transaction_date |
|----------------|--------|------------------|
| 1 | 150 | 2023-01-01 |
| 2 | 25 | 2023-01-01 |
| 3 | 30 | 2023-01-02 |
| 4 | 75 | 2023-01-02 |
โบ
Output:
| transaction_date | odd_sum | even_sum |
|------------------|---------|----------|
| 2023-01-01 | 25 | 150 |
| 2023-01-02 | 75 | 30 |
๐ก Note:
For 2023-01-01: amount 150 is even, amount 25 is odd. For 2023-01-02: amount 30 is even, amount 75 is odd. Results are sorted by date ascending.
example_2.sql โ Only Odd Transactions
$
Input:
transactions table:
| transaction_id | amount | transaction_date |
|----------------|--------|------------------|
| 1 | 15 | 2023-01-01 |
| 2 | 33 | 2023-01-01 |
| 3 | 7 | 2023-01-02 |
โบ
Output:
| transaction_date | odd_sum | even_sum |
|------------------|---------|----------|
| 2023-01-01 | 48 | 0 |
| 2023-01-02 | 7 | 0 |
๐ก Note:
All transactions have odd amounts. Even sums are 0 for both dates as required. 2023-01-01: 15+33=48 odd, 0 even. 2023-01-02: 7 odd, 0 even.
example_3.sql โ Mixed Edge Case
$
Input:
transactions table:
| transaction_id | amount | transaction_date |
|----------------|--------|------------------|
| 1 | 100 | 2023-01-01 |
| 2 | 200 | 2023-01-01 |
| 3 | 1 | 2023-01-03 |
| 4 | 2 | 2023-01-03 |
โบ
Output:
| transaction_date | odd_sum | even_sum |
|------------------|---------|----------|
| 2023-01-01 | 0 | 300 |
| 2023-01-03 | 1 | 2 |
๐ก Note:
2023-01-01 has only even amounts (100+200=300), so odd_sum is 0. 2023-01-03 has both odd (1) and even (2) amounts. Note there's no 2023-01-02 in the data.
Visualization
Tap to expand
Understanding the Visualization
1
Daily Transactions Flow In
Throughout the day, transactions with various amounts are processed
2
Smart Categorization
Each transaction is instantly categorized as odd or even based on amount
3
Daily Totals Calculated
At day's end, we have separate totals for odd and even amounts
4
Historical Report Generated
Results are sorted chronologically for trend analysis
Key Takeaway
๐ฏ Key Insight: SQL's conditional aggregation with CASE statements allows us to categorize and sum transactions in a single pass, making this solution both elegant and optimal for large datasets.
Time & Space Complexity
Time Complexity
O(n)
Single pass through n transactions with grouping
โ Linear Growth
Space Complexity
O(d)
Storage for d distinct dates in result set
โ Linear Space
Constraints
- 1 โค transaction_id โค 105
- 1 โค amount โค 106
- transaction_date is in YYYY-MM-DD format
- Each transaction_id is unique
- At least one transaction exists in the table
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code