Skip to content

Commit 0b0539d

Browse files
committed
task: #1179
1 parent d04480f commit 0b0539d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
125125
- [1075. Project Employees I](./leetcode/easy/1075.%20Project%20Employees%20I.sql)
126126
- [1141. User Activity for the Past 30 Days I](./leetcode/easy/1141.%20User%20Activity%20for%20the%20Past%2030%20Days%20I.sql)
127127
- [1148. Article Views I](./leetcode/easy/1148.%20Article%20Views%20I.sql)
128+
- [1179. Reformat Department Table](./leetcode/easy/1179.%20Reformat%20Department%20Table.sql)
128129
- [1211. Queries Quality and Percentage](./leetcode/easy/1211.%20Queries%20Quality%20and%20Percentage.sql)
129130
- [1251. Average Selling Price](./leetcode/easy/1251.%20Average%20Selling%20Price.sql)
130131
- [1280. Students and Examinations](./leetcode/easy/1280.%20Students%20and%20Examinations.sql)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Question 1179. Reformat Department Table
3+
Link: https://leetcode.com/problems/reformat-department-table/description/?envType=problem-list-v2&envId=database
4+
5+
Table: Department
6+
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| id | int |
11+
| revenue | int |
12+
| month | varchar |
13+
+-------------+---------+
14+
In SQL,(id, month) is the primary key of this table.
15+
The table has information about the revenue of each department per month.
16+
The month has values in ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"].
17+
18+
19+
Reformat the table such that there is a department id column and a revenue column for each month.
20+
21+
Return the result table in any order.
22+
*/
23+
24+
SELECT
25+
id,
26+
SUM(CASE WHEN month = 'Jan' THEN revenue END) AS Jan_Revenue,
27+
SUM(CASE WHEN month = 'Feb' THEN revenue END) AS Feb_Revenue,
28+
SUM(CASE WHEN month = 'Mar' THEN revenue END) AS Mar_Revenue,
29+
SUM(CASE WHEN month = 'Apr' THEN revenue END) AS Apr_Revenue,
30+
SUM(CASE WHEN month = 'May' THEN revenue END) AS May_Revenue,
31+
SUM(CASE WHEN month = 'Jun' THEN revenue END) AS Jun_Revenue,
32+
SUM(CASE WHEN month = 'Jul' THEN revenue END) AS Jul_Revenue,
33+
SUM(CASE WHEN month = 'Aug' THEN revenue END) AS Aug_Revenue,
34+
SUM(CASE WHEN month = 'Sep' THEN revenue END) AS Sep_Revenue,
35+
SUM(CASE WHEN month = 'Oct' THEN revenue END) AS Oct_Revenue,
36+
SUM(CASE WHEN month = 'Nov' THEN revenue END) AS Nov_Revenue,
37+
SUM(CASE WHEN month = 'Dec' THEN revenue END) AS Dec_Revenue
38+
FROM Department
39+
GROUP BY id

0 commit comments

Comments
 (0)