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
๐Ÿ’ฐ Smart Transaction Processing System๐Ÿ“… Day 12023-01-01$150, $25transactionsโšก Smart SorterIF amount % 2 = 1โ†’ Odd BinELSE โ†’ Even Bin๐Ÿ”ฅ Odd$25โญ Even$150๐Ÿ“… Day 22023-01-02$30, $75transactions๐Ÿ”ฅ Odd$75โญ Even$30๐Ÿ“Š Final ReportSorted by Date2023-01-01: Odd=$25, Even=$1502023-01-02: Odd=$75, Even=$30๐Ÿ’กKey Insight:One pass through data usingconditional aggregation!
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

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

Storage for d distinct dates in result set

n
2n
โœ“ 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
Asked in
JPMorgan 35 Goldman Sachs 28 Amazon 25 Microsoft 20 Google 18
28.5K Views
Medium Frequency
~12 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