Odd and Even Transactions - Problem

You are given a transactions table containing transaction details including transaction ID, amount, and date.

Goal: Calculate the sum of amounts for odd and even transactions for each day. If there are no odd or even transactions for a specific date, display as 0.

Return the result table ordered by transaction_date in ascending order.

Table Structure:

  • transaction_id (int): Unique identifier for each transaction
  • amount (int): Transaction amount
  • transaction_date (date): Date when transaction occurred

Table Schema

transactions
Column Name Type Description
transaction_id PK int Unique identifier for each transaction
amount int Transaction amount
transaction_date date Date when transaction occurred
Primary Key: transaction_id
Note: Each row represents a single transaction with unique transaction_id

Input & Output

Example 1 — Mixed Odd and Even Transactions
Input Table:
transaction_id amount transaction_date
2 150 2022-09-01
1 300 2022-09-01
13 600 2022-09-02
Output:
transaction_date odd_sum even_sum
2022-09-01 300 150
2022-09-02 600 0
💡 Note:

For 2022-09-01: transaction_id 1 (odd) has amount 300, transaction_id 2 (even) has amount 150. For 2022-09-02: only transaction_id 13 (odd) exists with amount 600, so even_sum is 0.

Example 2 — Only Even Transactions
Input Table:
transaction_id amount transaction_date
2 200 2022-09-01
4 400 2022-09-01
Output:
transaction_date odd_sum even_sum
2022-09-01 0 600
💡 Note:

Only even transaction_ids exist (2 and 4) with amounts 200 and 400 respectively, so odd_sum is 0 and even_sum is 600.

Example 3 — Multiple Dates with Different Patterns
Input Table:
transaction_id amount transaction_date
1 100 2022-09-01
2 200 2022-09-02
3 300 2022-09-02
Output:
transaction_date odd_sum even_sum
2022-09-01 100 0
2022-09-02 300 200
💡 Note:

2022-09-01 has only odd transaction (id=1, amount=100). 2022-09-02 has both odd (id=3, amount=300) and even (id=2, amount=200) transactions.

Constraints

  • 1 ≤ transaction_id ≤ 10^5
  • 1 ≤ amount ≤ 10^5
  • transaction_date is a valid date

Visualization

Tap to expand
Odd and Even Transaction Analysistransactions tableidamountdate215009-01130009-011360009-02Blue=Odd, Red=EvenGROUP BY dateSUM with CASEResultdateodd_sumeven_sum09-0130015009-026000Key: Use modulo (%) to check odd/even transaction IDstransaction_id % 2 = 1 → Odd | transaction_id % 2 = 0 → Even
Understanding the Visualization
1
Input
Transactions with ID, amount, and date
2
Group & Aggregate
Group by date, sum by ID parity
3
Output
Date with odd_sum and even_sum
Key Takeaway
🎯 Key Insight: Use conditional aggregation with modulo operator to separate and sum transactions by ID parity in a single query
Asked in
Amazon 12 Microsoft 8 Google 6
25.4K 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