-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCategory_Growth.sql
More file actions
20 lines (19 loc) · 1.27 KB
/
Copy pathCategory_Growth.sql
File metadata and controls
20 lines (19 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
--How did each product category’s sales grow year‑over‑year between 2012 and 2013?
SELECT
pc.Name AS ProductCategory,
YEAR(soh.OrderDate) AS SalesYear,
SUM(sod.LineTotal) AS TotalSales,
LAG(SUM(sod.LineTotal)) OVER (PARTITION BY pc.Name ORDER BY YEAR(soh.OrderDate)) AS PrevYearSales,
(SUM(sod.LineTotal) - LAG(SUM(sod.LineTotal)) OVER (PARTITION BY pc.Name ORDER BY YEAR(soh.OrderDate))) AS GrowthAmount,
((SUM(sod.LineTotal) - LAG(SUM(sod.LineTotal)) OVER (PARTITION BY pc.Name ORDER BY YEAR(soh.OrderDate))) * 100.0
/ LAG(SUM(sod.LineTotal)) OVER (PARTITION BY pc.Name ORDER BY YEAR(soh.OrderDate))) AS GrowthPercent
FROM Sales.SalesOrderHeader soh
JOIN Sales.SalesOrderDetail sod ON soh.SalesOrderID = sod.SalesOrderID
JOIN Production.Product p ON sod.ProductID = p.ProductID
JOIN Production.ProductSubcategory ps ON p.ProductSubcategoryID = ps.ProductSubcategoryID
JOIN Production.ProductCategory pc ON ps.ProductCategoryID = pc.ProductCategoryID
WHERE YEAR(soh.OrderDate) BETWEEN 2012 AND 2013
GROUP BY pc.Name, YEAR(soh.OrderDate)
ORDER BY pc.Name, SalesYear;
--Insight: Year‑over‑year analysis reveals which product categories expanded and which
--declined between 2012 and 2013, highlighting growth opportunities and areas needing attention.