From 971a8e2eb1feaed51d9dfad772e0acea1c1e3a99 Mon Sep 17 00:00:00 2001 From: Imran Imtiaz Date: Thu, 25 Jul 2024 08:37:05 +0400 Subject: [PATCH] Update 1179_Reformat_Department_Table.sql --- .../1179_Reformat_Department_Table.sql | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/PIVOT-UNPIVOT/1179_Reformat_Department_Table.sql b/PIVOT-UNPIVOT/1179_Reformat_Department_Table.sql index 53ade0c..a1e80e5 100644 --- a/PIVOT-UNPIVOT/1179_Reformat_Department_Table.sql +++ b/PIVOT-UNPIVOT/1179_Reformat_Department_Table.sql @@ -1,18 +1,20 @@ -SELECT id, - "Jan" as Jan_Revenue, - "Feb" as Feb_Revenue, - "Mar" as Mar_Revenue, - "Apr" as Apr_Revenue, - "May" as May_Revenue, - "Jun" as Jun_Revenue, - "Jul" as Jul_Revenue, - "Aug" as Aug_Revenue, - "Sep" as Sep_Revenue, - "Oct" as Oct_Revenue, - "Nov" as Nov_Revenue, - "Dec" as Dec_Revenue +-- Pivot table to display revenue for each month across departments +SELECT + id, + COALESCE([Jan], 0) AS Jan_Revenue, + COALESCE([Feb], 0) AS Feb_Revenue, + COALESCE([Mar], 0) AS Mar_Revenue, + COALESCE([Apr], 0) AS Apr_Revenue, + COALESCE([May], 0) AS May_Revenue, + COALESCE([Jun], 0) AS Jun_Revenue, + COALESCE([Jul], 0) AS Jul_Revenue, + COALESCE([Aug], 0) AS Aug_Revenue, + COALESCE([Sep], 0) AS Sep_Revenue, + COALESCE([Oct], 0) AS Oct_Revenue, + COALESCE([Nov], 0) AS Nov_Revenue, + COALESCE([Dec], 0) AS Dec_Revenue FROM department PIVOT ( MAX(revenue) - FOR month IN ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") + FOR month IN ([Jan], [Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov], [Dec]) ) AS pvt;