1
+ /*
2
+ Reformat Department Table
3
+ https://leetcode.com/problems/reformat-department-table
4
+
5
+ Table: Department
6
+
7
+ +---------------+---------+
8
+ | Column Name | Type |
9
+ +---------------+---------+
10
+ | id | int |
11
+ | revenue | int |
12
+ | month | varchar |
13
+ +---------------+---------+
14
+ (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
+ Write an SQL query to reformat the table such that there is a department id column and a revenue column for each month.
20
+
21
+ The query result format is in the following example:
22
+
23
+ Department table:
24
+ +------+---------+-------+
25
+ | id | revenue | month |
26
+ +------+---------+-------+
27
+ | 1 | 8000 | Jan |
28
+ | 2 | 9000 | Jan |
29
+ | 3 | 10000 | Feb |
30
+ | 1 | 7000 | Feb |
31
+ | 1 | 6000 | Mar |
32
+ +------+---------+-------+
33
+
34
+ Result table:
35
+ +------+-------------+-------------+-------------+-----+-------------+
36
+ | id | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue |
37
+ +------+-------------+-------------+-------------+-----+-------------+
38
+ | 1 | 8000 | 7000 | 6000 | ... | null |
39
+ | 2 | 9000 | null | null | ... | null |
40
+ | 3 | null | 10000 | null | ... | null |
41
+ +------+-------------+-------------+-------------+-----+-------------+
42
+
43
+ Note that the result table has 13 columns (1 for the department id + 12 for the months).
44
+ */
45
+
46
+
47
+
48
+
49
+ -- better solution
50
+ SELECT id,
51
+ sum (CASE WHEN month = " Jan" then revenue else NULL END) AS " Jan_Revenue" ,
52
+ sum (CASE WHEN month = " Feb" then revenue else NULL END) AS " Feb_Revenue" ,
53
+ sum (CASE WHEN month = " Mar" then revenue else NULL END) AS " Mar_Revenue" ,
54
+ sum (CASE WHEN month = " Apr" then revenue else NULL END) AS " Apr_Revenue" ,
55
+ sum (CASE WHEN month = " May" then revenue else NULL END) AS " May_Revenue" ,
56
+ sum (CASE WHEN month = " Jun" then revenue else NULL END) AS " Jun_Revenue" ,
57
+ sum (CASE WHEN month = " Jul" then revenue else NULL END) AS " Jul_Revenue" ,
58
+ sum (CASE WHEN month = " Aug" then revenue else NULL END) AS " Aug_Revenue" ,
59
+ sum (CASE WHEN month = " Sep" then revenue else NULL END) AS " Sep_Revenue" ,
60
+ sum (CASE WHEN month = " Oct" then revenue else NULL END) AS " Oct_Revenue" ,
61
+ sum (CASE WHEN month = " Nov" then revenue else NULL END) AS " Nov_Revenue" ,
62
+ sum (CASE WHEN month = " Dec" then revenue else NULL END) AS " Dec_Revenue"
63
+ FROM Department
64
+ GROUP BY id
65
+
66
+
67
+ -- bad solution
68
+ with jan AS (
69
+ SELECT id, revenue AS Jan_Revenue
70
+ FROM Department
71
+ WHERE month = " Jan"
72
+ ), feb AS (
73
+ SELECT id, revenue AS Feb_Revenue
74
+ FROM Department
75
+ WHERE month = " Feb"
76
+ ), mar AS (
77
+ SELECT id, revenue AS Mar_Revenue
78
+ FROM Department
79
+ WHERE month = " Mar"
80
+ ), apr AS (
81
+ SELECT id, revenue AS Apr_Revenue
82
+ FROM Department
83
+ WHERE month = " Apr"
84
+ ), may AS (
85
+ SELECT id, revenue AS May_Revenue
86
+ FROM Department
87
+ WHERE month = " May"
88
+ ), jun AS (
89
+ SELECT id, revenue AS Jun_Revenue
90
+ FROM Department
91
+ WHERE month = " Jun"
92
+ ), jul AS (
93
+ SELECT id, revenue AS Jul_Revenue
94
+ FROM Department
95
+ WHERE month = " Jul"
96
+ ), aug AS (
97
+ SELECT id, revenue AS Aug_Revenue
98
+ FROM Department
99
+ WHERE month = " Aug"
100
+ ), sep AS (
101
+ SELECT id, revenue AS Sep_Revenue
102
+ FROM Department
103
+ WHERE month = " Sep"
104
+ ), oct AS (
105
+ SELECT id, revenue AS Oct_Revenue
106
+ FROM Department
107
+ WHERE month = " Oct"
108
+ ), nov AS (
109
+ SELECT id, revenue AS Nov_Revenue
110
+ FROM Department
111
+ WHERE month = " Nov"
112
+ ), decemb AS (
113
+ SELECT id, revenue AS Dec_Revenue
114
+ FROM Department
115
+ WHERE month = " Dec"
116
+ )
117
+
118
+ 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
119
+ FROM Department LEFT JOIN jan using(id)
120
+ LEFT JOIN feb using(id)
121
+ LEFT JOIN mar using(id)
122
+ LEFT JOIN apr using(id)
123
+ LEFT JOIN may using(id)
124
+ LEFT JOIN jun using(id)
125
+ LEFT JOIN jul using(id)
126
+ LEFT JOIN aug using(id)
127
+ LEFT JOIN sep using(id)
128
+ LEFT JOIN oct using(id)
129
+ LEFT JOIN nov using(id)
130
+ LEFT JOIN decemb using(id)
131
+
0 commit comments