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๋ฅผ ์ปฌ๋ผ์ผ๋ก ํผ๋ดํด์ค๋ค.