-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReformat Department Table.sql
More file actions
19 lines (18 loc) · 995 Bytes
/
Reformat Department Table.sql
File metadata and controls
19 lines (18 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Write your MySQL query statement below
select
id,
sum(if(month = 'Jan', revenue, null)) as Jan_Revenue,
sum(if(month = 'Feb', revenue, null)) as Feb_Revenue,
sum(if(month = 'Mar', revenue, null)) as Mar_Revenue,
sum(if(month = 'Apr', revenue, null)) as Apr_Revenue,
sum(if(month = 'May', revenue, null)) as May_Revenue,
sum(if(month = 'Jun', revenue, null)) as Jun_Revenue,
sum(if(month = 'Jul', revenue, null)) as Jul_Revenue,
sum(if(month = 'Aug', revenue, null)) as Aug_Revenue,
sum(if(month = 'Sep', revenue, null)) as Sep_Revenue,
sum(if(month = 'Oct', revenue, null)) as Oct_Revenue,
sum(if(month = 'Nov', revenue, null)) as Nov_Revenue,
sum(if(month = 'Dec', revenue, null)) as Dec_Revenue
from Department
group by id;
#In this specific use case, SUM() is essentially returning the single non-NULL value for each department-month pair because each pair has at most one revenue entry. If there were multiple entries for the same department and month, the SUM() would add them together.