[mysql] LeetCode#1179. Reformat Department Table

kameals·2020년 2월 25일
2

leetcode

목록 보기
12/14
post-thumbnail

1. 문제

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.

The query result format is in the following example:
Department table:

+------+---------+-------+
| id   | revenue | month |
+------+---------+-------+
| 1    | 8000    | Jan   |
| 2    | 9000    | Jan   |
| 3    | 10000   | Feb   |
| 1    | 7000    | Feb   |
| 1    | 6000    | Mar   |
+------+---------+-------+

Result table:
+------+-------------+-------------+-------------+-----+-------------+
| id   | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue |
+------+-------------+-------------+-------------+-----+-------------+
| 1    | 8000        | 7000        | 6000        | ... | null        |
| 2    | 9000        | null        | null        | ... | null        |
| 3    | null        | 10000       | null        | ... | null        |
+------+-------------+-------------+-------------+-----+-------------+

Note that the result table has 13 columns (1 for the department id + 12 for the months).

해설

주어진 테이블을 다른 테이블로 재구성하기

2. 접근

// CASE WHEN 구문을 사용하자

3. 내가 작성한 답

# Write your MySQL query statement below

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
ORDER BY id

4. 다른 유저의 답안

대체로 비슷했다. 처음엔 내 답이 비효율적으로 보여서 loop를 돌릴 수 있는 방법이 있는지 찾았지만, 딱히 없는 것 같았고, 많은 투표를 얻은 유저들이 비슷한 방법을 사용하고 있었기 때문에, 이 방법으로 해결했다.

1. 아래는 MAX를 사용한 방법인데, SUM보다 2배 정도 빨랐다.

# Write your MySQL query statement below

SELECT id, 
	MAX(CASE WHEN month = 'Jan' THEN revenue END) AS Jan_Revenue,
	MAX(CASE WHEN month = 'Feb' THEN revenue END) AS Feb_Revenue,
	MAX(CASE WHEN month = 'Mar' THEN revenue END) AS Mar_Revenue,
	MAX(CASE WHEN month = 'Apr' THEN revenue END) AS Apr_Revenue,
	MAX(CASE WHEN month = 'May' THEN revenue END) AS May_Revenue,
	MAX(CASE WHEN month = 'Jun' THEN revenue END) AS Jun_Revenue,
	MAX(CASE WHEN month = 'Jul' THEN revenue END) AS Jul_Revenue,
	MAX(CASE WHEN month = 'Aug' THEN revenue END) AS Aug_Revenue,
	MAX(CASE WHEN month = 'Sep' THEN revenue END) AS Sep_Revenue,
	MAX(CASE WHEN month = 'Oct' THEN revenue END) AS Oct_Revenue,
	MAX(CASE WHEN month = 'Nov' THEN revenue END) AS Nov_Revenue,
	MAX(CASE WHEN month = 'Dec' THEN revenue END) AS Dec_Revenue
FROM department
GROUP BY id
ORDER BY id

2. if 를 사용한 유저

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;

5. 추가로 공부한 내용

GROUP BY 를 사용했기 때문에, SUM을 사용하여 null 인 경우도 대응할 수 있게 했으나, MAX 도 사용가능하다는 것을 알게 되었다.
다른 유저의 코멘트.

Each id and month pair only has at most 1 revenue, so MAX instead of SUM can work too.

그리고 else null 도 지울 수 있다는 코멘트도 있었다.

“else null” could be omitted

6. 참고사이트

profile
팀의 윤활유 역할이 되고 싶은 소박한 개발자입니다. 좌우명은 '밝고 바르고 튼튼하자'

0개의 댓글