-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregional-performance.sql
More file actions
90 lines (88 loc) · 3.87 KB
/
Copy pathregional-performance.sql
File metadata and controls
90 lines (88 loc) · 3.87 KB
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
-- Analysis: Regional performance
-- Business Question: Which product categories drive the highest sales and profit margins across different regions, and how have their sales trends changed over the past year (June 2013 - May 2014)?
-- SQL statement
WITH ProfitData AS (
SELECT
dl.TerritoryName,
dp.CategoryName,
SUM(fp.ProfitMargin) AS TotalProfitMargin
FROM
FactProductPerformance fp
JOIN
DimProduct dp ON fp.ProductID = dp.ProductID
JOIN
DimLocation dl ON fp.TerritoryID = dl.TerritoryID
JOIN
DimKeyDate dk ON fp.OrderDate = dk.DateKey
WHERE
dk.Year = 2013 AND dk.MonthName IN ('June', 'July', 'August', 'September', 'October', 'November', 'December')
OR dk.Year = 2014 AND dk.MonthName IN ('January', 'February', 'March', 'April', 'May')
GROUP BY
dl.TerritoryName, dp.CategoryName
),
ProfitPivot AS (
SELECT
TerritoryName,
ISNULL([Accessories], 0) AS Accessories_Profit,
ISNULL([Bikes], 0) AS Bikes_Profit,
ISNULL([Clothing], 0) AS Clothing_Profit,
ISNULL([Components], 0) AS Components_Profit,
ISNULL([Accessories], 0) + ISNULL([Bikes], 0) + ISNULL([Clothing], 0) + ISNULL([Components], 0) AS Total_Profit
FROM
ProfitData
PIVOT (
SUM(TotalProfitMargin)
FOR CategoryName IN ([Accessories], [Bikes], [Clothing], [Components])
) AS PVT
),
SalesData AS (
SELECT
dl.TerritoryName,
dp.CategoryName,
SUM(fp.ActualSaleValue) AS TotalSales
FROM
FactProductPerformance fp
JOIN
DimProduct dp ON fp.ProductID = dp.ProductID
JOIN
DimLocation dl ON fp.TerritoryID = dl.TerritoryID
JOIN
DimKeyDate dk ON fp.OrderDate = dk.DateKey
WHERE
dk.Year = 2013 AND dk.MonthName IN ('June', 'July', 'August', 'September', 'October', 'November', 'December')
OR dk.Year = 2014 AND dk.MonthName IN ('January', 'February', 'March', 'April', 'May')
GROUP BY
dl.TerritoryName, dp.CategoryName
),
SalesPivot AS (
SELECT
TerritoryName,
ISNULL([Accessories], 0) AS Accessories_Sales,
ISNULL([Bikes], 0) AS Bikes_Sales,
ISNULL([Clothing], 0) AS Clothing_Sales,
ISNULL([Components], 0) AS Components_Sales,
ISNULL([Accessories], 0) + ISNULL([Bikes], 0) + ISNULL([Clothing], 0) + ISNULL([Components], 0) AS Total_Sales
FROM
SalesData
PIVOT (
SUM(TotalSales)
FOR CategoryName IN ([Accessories], [Bikes], [Clothing], [Components])
) AS PVT
)
SELECT
P.TerritoryName,
P.Accessories_Profit,
P.Bikes_Profit,
P.Clothing_Profit,
P.Components_Profit,
P.Total_Profit,
CAST((S.Accessories_Sales / NULLIF(S.Total_Sales, 0)) * 100 AS DECIMAL(5,1)) AS [%Sales_Accessories],
CAST((S.Bikes_Sales / NULLIF(S.Total_Sales, 0)) * 100 AS DECIMAL(5,1)) AS [%Sales_Bikes],
CAST((S.Clothing_Sales / NULLIF(S.Total_Sales, 0)) * 100 AS DECIMAL(5,1)) AS [%Sales_Clothing],
CAST((S.Components_Sales / NULLIF(S.Total_Sales, 0)) * 100 AS DECIMAL(5,1)) AS [%Sales_Components]
FROM
ProfitPivot P
JOIN
SalesPivot S ON P.TerritoryName = S.TerritoryName
ORDER BY
P.Total_Profit DESC;