-
Notifications
You must be signed in to change notification settings - Fork 90
/
Reformat_Department_Table.sql
131 lines (115 loc) · 4.23 KB
/
Reformat_Department_Table.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Reformat Department Table
https://leetcode.com/problems/reformat-department-table
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).
*/
-- better solution
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
-- bad solution
with jan AS (
SELECT id, revenue AS Jan_Revenue
FROM Department
WHERE month = "Jan"
), feb AS (
SELECT id, revenue AS Feb_Revenue
FROM Department
WHERE month = "Feb"
), mar AS (
SELECT id, revenue AS Mar_Revenue
FROM Department
WHERE month = "Mar"
), apr AS (
SELECT id, revenue AS Apr_Revenue
FROM Department
WHERE month = "Apr"
), may AS (
SELECT id, revenue AS May_Revenue
FROM Department
WHERE month = "May"
), jun AS (
SELECT id, revenue AS Jun_Revenue
FROM Department
WHERE month = "Jun"
), jul AS (
SELECT id, revenue AS Jul_Revenue
FROM Department
WHERE month = "Jul"
), aug AS (
SELECT id, revenue AS Aug_Revenue
FROM Department
WHERE month = "Aug"
), sep AS (
SELECT id, revenue AS Sep_Revenue
FROM Department
WHERE month = "Sep"
), oct AS (
SELECT id, revenue AS Oct_Revenue
FROM Department
WHERE month = "Oct"
), nov AS (
SELECT id, revenue AS Nov_Revenue
FROM Department
WHERE month = "Nov"
), decemb AS (
SELECT id, revenue AS Dec_Revenue
FROM Department
WHERE month = "Dec"
)
SELECT Distinct(Department.id), jan.Jan_Revenue, feb.Feb_Revenue, mar.Mar_Revenue, apr.Apr_Revenue, may.May_Revenue, jun.Jun_Revenue, jul.Jul_Revenue, aug.Aug_Revenue, sep.Sep_Revenue, oct.Oct_Revenue, nov.Nov_Revenue, decemb.Dec_Revenue
FROM Department LEFT JOIN jan using(id)
LEFT JOIN feb using(id)
LEFT JOIN mar using(id)
LEFT JOIN apr using(id)
LEFT JOIN may using(id)
LEFT JOIN jun using(id)
LEFT JOIN jul using(id)
LEFT JOIN aug using(id)
LEFT JOIN sep using(id)
LEFT JOIN oct using(id)
LEFT JOIN nov using(id)
LEFT JOIN decemb using(id)