[코드카타] SQL 35 Monthly Transactions I

Data_Student·2024년 11월 18일
0

코드카타

목록 보기
44/57

[코드카타] SQL 35 Monthly Transactions I

35. Monthly Transactions I
https://leetcode.com/problems/monthly-transactions-i/

Write an SQL query to find for each month and country, the number of 
transactions and their total amount, the number of approved transactions 
and their total amount.
Return the result table in any order.
select date_format(trans_date, '%Y-%m') month, 
        country, 
        count(*) trans_count,  # *를 사용하여 null 값도 count 하기!
        count(case when state = 'approved' then 1 end) approved_count, 
        sum(amount) trans_total_amount, 
        sum(case when state = 'approved' then amount else 0 end) approved_total_amount
from Transactions
group by 1, 2
count() 함수와 sum() 함수 내에 조건을 통한 원하는 값 추출

0개의 댓글