개발은 처음이라 개발새발
My SQL CASE문 퀴즈풀기-2 본문
이번에도 CASE문 퀴즈를 풀어보려고 합니다. 이번에는 해커랭크가 아니라 리트코드의 퀴즈를 풀어볼 겁니다.
https://leetcode.com/problems/reformat-department-table/
Reformat Department Table - LeetCode
Can you solve this real interview question? Reformat Department Table - Table: Department +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | revenue | int | | month | varchar | +-------------+---------+ (id, month) is
leetcode.com

바로 위 그림이 문제인데요. Department 테이블을 활용해서 월간 Reveune를 구해야 합니다. 그리고 중요한 키포인트로 보이는 것이 있는데 바로 맨 왼쪽에 'id'가 눈에 띄네요. Input 테이블을 보면 로우가 5개고 1인 id가 3개가 있는데요. 이를 봐서 Output 테이블은 Group By를 적용해야 하는 것으로 판단됩니다. 그리고 월별 Revenue는 id를 기준으로 month별 합산으로 구성된 것을 알 수 있습니다. 이제 파악이 다 끝났으니 쿼리를 짜보겠습니다.
SELECT id,
SUM(CASE WHEN month = 'Jan' THEN Revenu ELSE NULL END)AS Jan_Revenue
FROM Department
GROUP BY id
| id | Jan_revenue |
| -- | ----------- |
| 1 | 8000 |
| 2 | 9000 |
| 3 | null |
month = 'Jan'일 때를 조건으로 쿼리를 짜보았는데요. 문제의 Output과 비슷한 구조를 보이고 있습니다. 조건문의 구조를 좀 더 설명해보자면 month가 Jan일 때 revenue를 결과물로 도출하고 예외의 경우 NULL로 처리하는 것으로 하고 이를 합산하는 것으로 처리했습니다. 그렇기 때문에 Jan이 없는 id =3은 null로 나온 것을 확인할 수 있습니다. 하지만 결과물은 12월까지 이기에 1월 처럼 12줄을 만들면 되겠습니다.
SELECT id,
SUM(CASE WHEN month = 'Jan' then revenue else null end) as Jan_revenue,
SUM(CASE WHEN month = 'Feb' then revenue else null end) as Feb_revenue,
SUM(CASE WHEN month = 'Mar' then revenue else null end) as Mar_revenue,
SUM(CASE WHEN month = 'Apr' then revenue else null end) as Apr_revenue,
SUM(CASE WHEN month = 'May' then revenue else null end) as May_revenue,
SUM(CASE WHEN month = 'Jun' then revenue else null end) as Jun_revenue,
SUM(CASE WHEN month = 'Jul' then revenue else null end) as Jul_revenue,
SUM(CASE WHEN month = 'Aug' then revenue else null end) as Aug_revenue,
SUM(CASE WHEN month = 'Sep' then revenue else null end) as Sep_revenue,
SUM(CASE WHEN month = 'Oct' then revenue else null end) as Oct_revenue,
SUM(CASE WHEN month = 'Nov' then revenue else null end) as Nov_revenue,
SUM(CASE WHEN month = 'Dec' then revenue else null end) as Dec_revenue
FROM Department
GROUP BY id
| id | Jan_Revenue | Feb_Revenue | Mar_Revenue | Apr_Revenue | May_Revenue | Jun_Revenue | Jul_Revenue | Aug_Revenue | Sep_Revenue | Oct_Revenue | Nov_Revenue | Dec_Revenue |
| -- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- |
| 1 | 8000 | 7000 | 6000 | null | null | null | null | null | null | null | null | null |
| 2 | 9000 | null | null | null | null | null | null | null | null | null | null | null |
| 3 | null | 10000 | null | null | null | null | null | null | null | null | null | null |
네 이렇게 1월부터 12월까지의 id별 합산 revenue를 구해봤습니다.
'mysql' 카테고리의 다른 글
| Left Join 퀴즈 풀어보기 [LeetCode] (3) | 2023.05.06 |
|---|---|
| JOIN을 이용해 대륙별 평균 도시의 인구수를 구하라 [해커랭크] (0) | 2023.05.01 |
| My SQL CASE문 삼각형 종류 퀴즈로 연습하기[해커랭크] (1) | 2023.04.30 |
| 해커랭크 Top Earners 문제 풀이 [MySQL] (0) | 2023.04.23 |
| [해커랭크] MySQL 집계함수 문제 풀이 (0) | 2023.04.22 |