SQL

[Leetcode/SQL] 1179. Reformat Department Table

지구인 ㅣ 2022. 8. 25. 15:42

728x90

문제는 여기서 볼 수 있습니다.

 

Table: Department

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| revenue     | int     |
| month       | varchar |
+-------------+---------+
(id, month) is the primary key of this table.
The table has information about the revenue of each department per month.
The month has values in ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].

 

Write an SQL query to reformat the table such that there is a department id column and a revenue column for each month.

Return the result table in any order.

The query result format is in the following example.

 

Example 1:

Input: 
Department table:
+------+---------+-------+
| id   | revenue | month |
+------+---------+-------+
| 1    | 8000    | Jan   |
| 2    | 9000    | Jan   |
| 3    | 10000   | Feb   |
| 1    | 7000    | Feb   |
| 1    | 6000    | Mar   |
+------+---------+-------+
Output: 
+------+-------------+-------------+-------------+-----+-------------+
| id   | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue |
+------+-------------+-------------+-------------+-----+-------------+
| 1    | 8000        | 7000        | 6000        | ... | null        |
| 2    | 9000        | null        | null        | ... | null        |
| 3    | null        | 10000       | null        | ... | null        |
+------+-------------+-------------+-------------+-----+-------------+
Explanation: The revenue from Apr to Dec is null.
Note that the result table has 13 columns (1 for the department id + 12 for the months).

 

Code:

/*
- 12월 모두 일일이 컬럼으로 설정해주어야 한다
- sum 혹은 max를 사용하지 않을 시 -> 각 id의 row 개수만큼 같은 id에 대한 row가 생긴다.
하지만 문제에서는 id로 grouping해서 보여주길 바라므로 id가 distinct해야한다.
*/

# case 1

select id,
sum(if(month='Jan',revenue,null)) as Jan_Revenue,
sum(if(month='Feb',revenue,null)) as Feb_Revenue,
sum(if(month='Mar',revenue,null)) as Mar_Revenue,
sum(if(month='Apr',revenue,null)) as Apr_Revenue,
sum(if(month='May',revenue,null)) as May_Revenue,
sum(if(month='Jun',revenue,null)) as Jun_Revenue,
sum(if(month='Jul',revenue,null)) as Jul_Revenue,
sum(if(month='Aug',revenue,null)) as Aug_Revenue,
sum(if(month='Sep',revenue,null)) as Sep_Revenue,
sum(if(month='Oct',revenue,null)) as Oct_Revenue,
sum(if(month='Nov',revenue,null)) as Nov_Revenue,
sum(if(month='Dec',revenue,null)) as Dec_Revenue
from Department
group by id;

# case 2

select id, 
MAX(CASE WHEN month = 'Jan' THEN revenue else null END) AS Jan_Revenue,
MAX(CASE WHEN month = 'Feb' THEN revenue else null END) AS Feb_Revenue,
MAX(CASE WHEN month = 'Mar' THEN revenue else null END) AS Mar_Revenue,
MAX(CASE WHEN month = 'Apr' THEN revenue else null END) AS Apr_Revenue,
MAX(CASE WHEN month = 'May' THEN revenue else null END) AS May_Revenue,
MAX(CASE WHEN month = 'Jun' THEN revenue else null END) AS Jun_Revenue,
MAX(CASE WHEN month = 'Jul' THEN revenue else null END) AS Jul_Revenue,
MAX(CASE WHEN month = 'Aug' THEN revenue else null END) AS Aug_Revenue,
MAX(CASE WHEN month = 'Sep' THEN revenue else null END) AS Sep_Revenue,
MAX(CASE WHEN month = 'Oct' THEN revenue else null END) AS Oct_Revenue,
MAX(CASE WHEN month = 'Nov' THEN revenue else null END) AS Nov_Revenue,
MAX(CASE WHEN month = 'Dec' THEN revenue else null END) AS Dec_Revenue
from Department
group by 1;

 

설명은 위의 코드에 주석으로 달았다.

case when then else 구문을 잘 안 써버릇해서 스스로 코드 짜는 데에 잘 사용하지 못하는 것 같다. 관련 문제를 많이 풀어봐야겠다.

728x90