Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[LeetCode] 1179. Reformat Department Table

์ธ” 2022. 8. 26. 17:56

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š”

       ๋ฆฌํŠธ์ฝ”๋“œ Problems, https://leetcode.com/problemset/all/ ์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.


โ–  ๋ฌธ์ œ

https://leetcode.com/problems/reformat-department-table/

 

Reformat Department Table - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

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.

 


โ–  ํ’€์ด

SELECT id,
       SUM(CASE WHEN month = 'Jan' Then Revenue END) Jan_Revenue,
       SUM(CASE WHEN month = 'Feb' Then Revenue END) Feb_Revenue,
       SUM(CASE WHEN month = 'Mar' Then Revenue END) Mar_Revenue,       
       SUM(CASE WHEN month = 'Apr' Then Revenue END) Apr_Revenue,
       SUM(CASE WHEN month = 'May' Then Revenue END) May_Revenue,
       SUM(CASE WHEN month = 'Jun' Then Revenue END) Jun_Revenue,
       SUM(CASE WHEN month = 'Jul' Then Revenue END) Jul_Revenue,
       SUM(CASE WHEN month = 'Aug' Then Revenue END) Aug_Revenue,
       SUM(CASE WHEN month = 'Sep' Then Revenue END) Sep_Revenue,      
       SUM(CASE WHEN month = 'Oct' Then Revenue END) Oct_Revenue,       
       SUM(CASE WHEN month = 'Nov' Then Revenue END) Nov_Revenue,
       SUM(CASE WHEN month = 'Dec' Then Revenue END) Dec_Revenue
       
       FROM Department
 GROUP BY id;

id ์ปฌ๋Ÿผ์œผ๋กœ ๊ทธ๋ฃนํ•‘ํ•ด์„œ id ๋ณ„๋กœ month๋ณ„ revnue๋ฅผ ์ปฌ๋Ÿผ์œผ๋กœ ํ”ผ๋ด‡ํ•ด์ค€๋‹ค.