[leetcode]1193. Monthly Transactions I

김준석·2024년 2월 6일
0

코딩테스트 - SQL

목록 보기
67/96

문제

https://leetcode.com/problems/monthly-transactions-i/description/?envType=study-plan-v2&envId=top-sql-50

코드

SELECT
    DATE_FORMAT(trans_date,'%Y-%m') AS month,
    country,
    COUNT(*) AS trans_count,
    SUM(CASE WHEN state='approved' THEN 1 ELSE 0 END) AS approved_count,
    SUM(amount) AS trans_total_amount,
    SUM(CASE WHEN state='approved' THEN amount ELSE 0 END) AS approved_total_amount
FROM
    Transactions
GROUP BY
    month,
    country

풀이

해당 문제의 국가는 null 이 존재함. 하지만 나라는 null이더라도 다른 것은 다 적혀있다.

그렇기 때문에 trans_count 를 전체로 잡아 줘야 됨.
COUNT(*) = NULL 값도 카운트함
COUNT(컬럼명) = NULL 값은 카운트 안함.

0개의 댓글