[SQL 문제 풀이] Odd and Even Transactions (홀수 및 짝수 거래)

Stupefyee's avatar
May 27, 2025
[SQL 문제 풀이] Odd and Even Transactions (홀수 및 짝수 거래)
Odd and Even Transactions - LeetCode
Can you solve this real interview question? Odd and Even Transactions - Table: transactions +------------------+------+ | Column Name | Type | +------------------+------+ | transaction_id | int | | amount | int | | transaction_date | date | +------------------+------+ The transactions_id column uniquely identifies each row in this table. Each row of this table contains the transaction id, amount and transaction date. Write a solution to find 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. The result format is in the following example.   Example: Input: transactions table: +----------------+--------+------------------+ | transaction_id | amount | transaction_date | +----------------+--------+------------------+ | 1 | 150 | 2024-07-01 | | 2 | 200 | 2024-07-01 | | 3 | 75 | 2024-07-01 | | 4 | 300 | 2024-07-02 | | 5 | 50 | 2024-07-02 | | 6 | 120 | 2024-07-03 | +----------------+--------+------------------+ Output: +------------------+---------+----------+ | transaction_date | odd_sum | even_sum | +------------------+---------+----------+ | 2024-07-01 | 75 | 350 | | 2024-07-02 | 0 | 350 | | 2024-07-03 | 0 | 120 | +------------------+---------+----------+ Explanation: * For transaction dates: * 2024-07-01: * Sum of amounts for odd transactions: 75 * Sum of amounts for even transactions: 150 + 200 = 350 * 2024-07-02: * Sum of amounts for odd transactions: 0 * Sum of amounts for even transactions: 300 + 50 = 350 * 2024-07-03: * Sum of amounts for odd transactions: 0 * Sum of amounts for even transactions: 120 Note: The output table is ordered by transaction_date in ascending order.
Odd and Even Transactions - LeetCode
notion image
홀수 및 짝수 거래의 금액 합계를 구하는 솔루션을 작성합니다. 특정 날짜에 홀수 또는 짝수 거래가 없는 경우 0으로 표시합니다. 트랜잭션_날짜별로 정렬된 결과 테이블을 오름차순으로 반환합니다.
 

내가 작성한 쿼리

Oracle

SELECT TO_CHAR(transaction_date, 'YYYY-MM-DD') AS TRANSACTION_DATE, SUM(CASE WHEN MOD(amount, 2) = 1 THEN amount ELSE 0 END) AS ODD_SUM, SUM(CASE WHEN MOD(amount, 2) = 0 THEN amount ELSE 0 END) AS EVEN_SUM FROM transactions GROUP BY transaction_date ORDER BY transaction_date;

MySQL

SELECT transaction_date, SUM(CASE WHEN MOD(amount, 2) = 1 THEN amount ELSE 0 END) AS ODD_SUM, SUM(CASE WHEN MOD(amount, 2) = 0 THEN amount ELSE 0 END) AS EVEN_SUM FROM transactions GROUP BY transaction_date ORDER BY transaction_date;

차이점

  • 날짜 표기 방식 차이
Share article

stupefyee