-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathsales_analysis.sql
More file actions
110 lines (90 loc) · 2.64 KB
/
sales_analysis.sql
File metadata and controls
110 lines (90 loc) · 2.64 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
/* =========================================
AdventureWorks Internet Sales Analysis
SQL Project
========================================= */
/* 1) Total Internet Sales Revenue */
SELECT
SUM(SalesAmount) AS Total_Internet_Sales
FROM dbo.FactInternetSales;
/* 2) Top 10 Revenue Generating Products */
SELECT TOP 10
p.EnglishProductName,
SUM(f.SalesAmount) AS TotalSales
FROM dbo.FactInternetSales f
JOIN dbo.DimProduct p
ON f.ProductKey = p.ProductKey
GROUP BY p.EnglishProductName
ORDER BY TotalSales DESC;
/* 3️) Sales Trend by Year */
SELECT
d.CalendarYear,
SUM(f.SalesAmount) AS TotalSales
FROM dbo.FactInternetSales f
JOIN dbo.DimDate d
ON f.OrderDateKey = d.DateKey
GROUP BY d.CalendarYear
ORDER BY d.CalendarYear;
/* 4️) Top 10 Customers by Revenue */
SELECT TOP 10
c.FirstName,
c.LastName,
SUM(f.SalesAmount) AS CustomerSales
FROM dbo.FactInternetSales f
JOIN dbo.DimCustomer c
ON f.CustomerKey = c.CustomerKey
GROUP BY c.FirstName, c.LastName
ORDER BY CustomerSales DESC;
/* 5️) Sales by Country */
SELECT
g.EnglishCountryRegionName,
SUM(f.SalesAmount) AS TotalSales
FROM dbo.FactInternetSales f
JOIN dbo.DimCustomer c
ON f.CustomerKey = c.CustomerKey
JOIN dbo.DimGeography g
ON c.GeographyKey = g.GeographyKey
GROUP BY g.EnglishCountryRegionName
ORDER BY TotalSales DESC;
/* 6️) Sales by Product Category */
SELECT
pc.EnglishProductCategoryName,
SUM(f.SalesAmount) AS TotalSales
FROM dbo.FactInternetSales f
JOIN dbo.DimProduct p
ON f.ProductKey = p.ProductKey
JOIN dbo.DimProductSubcategory ps
ON p.ProductSubcategoryKey = ps.ProductSubcategoryKey
JOIN dbo.DimProductCategory pc
ON ps.ProductCategoryKey = pc.ProductCategoryKey
GROUP BY pc.EnglishProductCategoryName
ORDER BY TotalSales DESC;
/* 7️) Internet Sales in Year 2013 */
SELECT
SUM(f.SalesAmount) AS Sales_2013
FROM dbo.FactInternetSales f
JOIN dbo.DimDate d
ON f.OrderDateKey = d.DateKey
WHERE d.CalendarYear = 2013;
/* 8️) Average Order Value */
SELECT
AVG(SalesAmount) AS Avg_Order_Value
FROM dbo.FactInternetSales;
/* 9️) Total Quantity Sold by Product */
SELECT TOP 10
p.EnglishProductName,
SUM(f.OrderQuantity) AS TotalQuantitySold
FROM dbo.FactInternetSales f
JOIN dbo.DimProduct p
ON f.ProductKey = p.ProductKey
GROUP BY p.EnglishProductName
ORDER BY TotalQuantitySold DESC;
/* 10) Monthly Sales Trend */
SELECT
d.CalendarYear,
d.MonthNumberOfYear,
SUM(f.SalesAmount) AS MonthlySales
FROM dbo.FactInternetSales f
JOIN dbo.DimDate d
ON f.OrderDateKey = d.DateKey
GROUP BY d.CalendarYear, d.MonthNumberOfYear
ORDER BY d.CalendarYear, d.MonthNumberOfYear;