출처
문제
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 in ascending order.transaction_date
문제에 대한 해석
![]() |
![]() |
거래 날 별로 거래수량을 홀수인 경우와 짝수인 경우의 합을 계산
2024-07-01의 홀수 거래량 : 75
2024-07-01의 짝수 거래량 : 200 + 150
풀이(MYSQL)
-- 정답
with base as (
select transaction_date,
if(amount%2 = 1 , amount, 0 ) as'odd_sum',
if(amount%2 = 0 , amount, 0 ) as'even_sum'
from transactions
)
select transaction_date, sum(odd_sum) as 'odd_sum', sum(even_sum) as 'even_sum'
from base
group by transaction_date
order by transaction_date
select transaction_date,
sum(if(amount%2 = 1 , amount, 0 )) as'odd_sum',
sum(if(amount%2 = 0 , amount, 0 )) as'even_sum'
좀더 간단하게 하기 위해 if절에 sum()의 집계함수를 사용가능
'문제풀이 > SQL(My sql)' 카테고리의 다른 글
[문제풀이] Leet code - easy2 (0) | 2024.08.15 |
---|---|
[문제풀이] Leet code - Last Person to Fit in the Bus (0) | 2024.08.13 |
[문제풀이] Leet code - Immediate Food Delivery II (0) | 2024.08.13 |
[문제풀이] Leet code - easy (0) | 2024.08.08 |
[문제풀이] Leet code - Sales Person (0) | 2024.08.07 |