Repository navigation
Expand file tree
/
Copy pathPerformanceAnalysis.sql
More file actions
148 lines (131 loc) · 4.49 KB
/
Copy pathPerformanceAnalysis.sql
File metadata and controls
148 lines (131 loc) · 4.49 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
-- 1) Find the top three high performing products within each category for every year and rank them.
WITH ProdRevenue AS (
SELECT
YEAR(s.order_date) AS YearOfSale,
p.category_id,
p.product_id,
SUM(s.sales_amount) AS SalesFromProduct
FROM gold.fact_sales AS s
INNER JOIN gold.dim_products AS p ON s.product_key = p.product_key
WHERE s.order_date IS NOT NULL
GROUP BY YEAR(s.order_date), p.category_id, p.product_id
),
ProdRanking AS (
SELECT
*,
DENSE_RANK() OVER(PARTITION BY YearOfSale, category_id ORDER BY SalesFromProduct DESC) AS DenseRank,
ROW_NUMBER() OVER(PARTITION BY YearOfSale, category_id ORDER BY SalesFromProduct DESC) AS RowNumber
FROM ProdRevenue
)
SELECT *
FROM ProdRanking
WHERE RowNumber <=3;
--2) Find the products whose revenue is above average revenue of each category for year 2011.
WITH ProdRevenue AS (
SELECT
YEAR(s.order_date) AS YearOfSale,
p.category_id,
s.product_key,
SUM(s.sales_amount) AS SalesFromProd
FROM gold.fact_sales AS s
INNER JOIN gold.dim_products AS p ON p.product_key = s.product_key
WHERE s.order_date IS NOT NULL
AND s.order_date BETWEEN '2011-01-01' AND '2011-12-31'
GROUP BY YEAR(s.order_date), p.category_id, s.product_key
),
AverageRevenue AS (
SELECT
*,
AVG(SalesFromProd) OVER(PARTITION BY category_id) AS CatAverage
FROM ProdRevenue
)
SELECT *
FROM AverageRevenue
WHERE SalesFromProd > CatAverage;
--3) Find the % difference in sales of next year from the sales of previous year for each category.
WITH YearWiseSales AS (
SELECT
YEAR(s.order_date) AS YearOfSale,
p.category_id,
SUM(s.sales_amount) AS RevenueByCategory
FROM gold.fact_sales AS s
INNER JOIN gold.dim_products AS p ON p.product_key = s.product_key
WHERE s.order_date IS NOT NULL
GROUP BY YEAR(s.order_date), p.category_id
),
NextYearSales AS (
SELECT
*,
LEAD(RevenueByCategory) OVER(PARTITION BY category_id ORDER BY YearOfSale) AS SaleOfNextYear
FROM YearWiseSales
)
SELECT
*,
((SaleOfNextYear - RevenueByCategory) * 100.0 / RevenueByCategory) AS PercentDiff
FROM NextYearSales
ORDER BY YearOfSale, category_id;
--4) Find the highest revenue generating product within each category and compare its revenue with other product's revenue for year 2011.
WITH ProdRevenue AS (
SELECT
YEAR(s.order_date) AS YearOfSale,
p.category_id,
s.product_key,
SUM(s.sales_amount) AS SalesFromProd
FROM gold.fact_sales AS s
INNER JOIN gold.dim_products AS p ON p.product_key = s.product_key
WHERE s.order_date IS NOT NULL
AND s.order_date BETWEEN '2011-01-01' AND '2011-12-31'
GROUP BY YEAR(s.order_date), p.category_id, s.product_key
)
SELECT
*,
FIRST_VALUE(SalesFromProd) OVER(PARTITION BY category_id ORDER BY SalesFromProd DESC) AS TopProdRev,
FIRST_VALUE(SalesFromProd) OVER(PARTITION BY category_id ORDER BY SalesFromProd DESC) - SalesFromProd AS DiffInRevenue
FROM ProdRevenue
ORDER BY YearOfSale, category_id, SalesFromProd DESC;
--5) Classify the customers into three segments based on the revenue earned through them for every year.
WITH RevenueByCustomers AS (
SELECT
YEAR(order_date) AS YearOfSale,
customer_key,
SUM(sales_amount) AS SalesThroughCustomers
FROM gold.fact_sales
WHERE order_date IS NOT NULL
GROUP BY YEAR(order_date), customer_key
),
CustomerSegments AS (
SELECT
*,
NTILE(3) OVER(PARTITION BY YearOfSale ORDER BY SalesThroughCustomers DESC) AS CustSegment
FROM RevenueByCustomers
)
SELECT
*,
CASE
WHEN CustSegment = 1 THEN 'Diamond'
WHEN CustSegment = 2 THEN 'Gold'
ELSE 'Silver'
END AS CustomerType
FROM CustomerSegments
ORDER BY YearOfSale;
--6) Find the top 25% products by revenues for every year.
WITH RevenueByProducts AS (
SELECT
YEAR(s.order_date) AS YearOfSale,
p.product_key,
p.product_name,
SUM(s.sales_amount) AS SalesByProduct
FROM gold.fact_sales AS s
INNER JOIN gold.dim_products AS p ON p.product_key = s.product_key
WHERE s.order_date IS NOT NULL
GROUP BY YEAR(s.order_date), p.product_key, p.product_name
) ,
ProdContrInRevenue AS (
SELECT
*,
CUME_DIST() OVER(PARTITION BY YearOfSale ORDER BY SalesByProduct DESC) AS RevenueContribution
FROM RevenueByProducts
)
SELECT *
FROM ProdContrInRevenue
WHERE RevenueContribution <= .25;