-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPP_SQL4_SuperStoreDW_Query.sql
More file actions
238 lines (170 loc) · 5.48 KB
/
SPP_SQL4_SuperStoreDW_Query.sql
File metadata and controls
238 lines (170 loc) · 5.48 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
-- 1.Creating view to show top 5 states by sales.
USE SuperShopDB
GO
DROP VIEW IF EXISTS vTop5StatesAndSales
GO
CREATE VIEW vTop5StatesAndSales
AS
SELECT TOP 5 [State], [Sales]
FROM [dbo].[SuperShop ]
ORDER BY [Sales] DESC
GO
SELECT * FROM vTop5StatesAndSales;
-------------------------------------------------------------------------------------
-- 2. Creating view to show last 5 states by sales.
USE SuperShopDB
GO
DROP VIEW IF EXISTS vLast5StatesAndSales
GO
CREATE VIEW vLast5StatesAndSales
AS
SELECT TOP 5 [State], [Sales]
FROM [dbo].[SuperShop ]
ORDER BY [Sales] ASC
GO
SELECT * FROM vLast5StatesAndSales;
-------------------------------------------------------------------------------------
-- 3.Creating view to show top 5 City by sales.
USE SuperShopDB
GO
DROP VIEW IF EXISTS vTop5CityAndSales
GO
CREATE VIEW vTop5CityAndSales
AS
SELECT TOP 5 [City], [Sales]
FROM [dbo].[SuperShop ]
ORDER BY [Sales] DESC
GO
SELECT * FROM vTop5CityAndSales;
-------------------------------------------------------------------------------------
-- 4.Creating view to show last 5 City by sales.
USE SuperShopDB
GO
DROP VIEW IF EXISTS vLast5CityAndSales
GO
CREATE VIEW vLast5CityAndSales
AS
SELECT TOP 5 [City], [Sales]
FROM [dbo].[SuperShop ]
ORDER BY [Sales] ASC
GO
SELECT * FROM vLast5CityAndSales;
-------------------------------------------------------------------------------------
-- 5. Average Sales By Post Code
USE SuperShopDB
GO
DROP PROCEDURE IF EXISTS spAverageSalesByPostCode
GO
CREATE PROCEDURE spAverageSalesByPostCode (@PostCode INT)
AS
SELECT [Postal_Code], ROUND(AVG([Sales]), 2) AS 'Average Sales by Postcode'
FROM [dbo].[SuperShop ]
WHERE [Postal_Code] = @PostCode
GROUP BY [Postal_Code]
GO
EXEC spAverageSalesByPostCode 77041;
-------------------------------------------------------------------------------------
-- 6. Top 5 Products by Profit and Year
Use SuperShopDB
GO
DROP PROCEDURE IF EXISTS spTop5ProductsByYearAndProfit
GO
CREATE PROCEDURE spTop5ProductsByYearAndProfit (@Year INT)
AS
SELECT TOP 5 [Product_Name], [Profit]
FROM [dbo].[SuperShop]
WHERE YEAR([Order_Date]) = @Year
ORDER BY [Profit] DESC
GO
EXEC spTop5ProductsByYearAndProfit 2014;
-------------------------------------------------------------------------------------
-- 7. Top 5 Products by Sales and Year
USE SuperShopDB
GO
DROP PROCEDURE IF EXISTS spTop5ProductsByYearAndSale
GO
CREATE PROCEDURE spTop5ProductsByYearAndSales (@Year INT)
AS
SELECT TOP 5 [Product_Name], [Sales]
FROM [dbo].[SuperShop]
WHERE YEAR([Order_Date]) = @Year
ORDER BY [Sales] DESC
GO
EXEC spTop5ProductsByYearAndSales 2014;
-------------------------------------------------------------------------------------
-- 8. Top 10 Products by Total Orders, Year and State
USE SuperShopDB
GO
DROP PROCEDURE IF EXISTS spTop10ProductsByYearAndState
GO
CREATE PROCEDURE spTop10ProductsByYearAndState (@YEAR INT, @STATE VARCHAR(50))
AS
SELECT TOP 10 [Product_Name], COUNT([Order_ID]) AS 'TOTAL ORDERS'
FROM [dbo].[SuperShop]
WHERE YEAR([Order_Date]) = @YEAR AND [State] = @STATE
GROUP BY [Product_Name]
ORDER BY COUNT([Order_ID]) DESC
GO
EXEC spTop10ProductsByYearAndState 2014, 'California';
-------------------------------------------------------------------------------------
-- 9. Top 10 Products by Total Orders, Year and City
USE SuperShopDB
GO
DROP PROCEDURE IF EXISTS spTop10ProductsByYearAndCity
GO
CREATE PROCEDURE spTop10ProductsByYearAndCity (@YEAR INT, @CITY VARCHAR(50))
AS
SELECT TOP 10 [Product_Name], COUNT([Order_ID]) AS 'TOTAL ORDERS'
FROM [dbo].[SuperShop]
WHERE YEAR([Order_Date]) = @YEAR AND [City] = @CITY
GROUP BY [Product_Name]
ORDER BY COUNT([Order_ID]) DESC
GO
EXEC spTop10ProductsByYearAndCity 2014, 'Los Angeles';
-------------------------------------------------------------------------------------
-- 10. Highest Profit by Consumer Segment
USE SuperShopDB
GO
DROP PROCEDURE IF EXISTS spTopProfitConsumerByYear
GO
CREATE PROCEDURE spTopProfitConsumerByYear (@YEAR INT)
AS
SELECT TOP 1 [Segment], [Profit]
FROM [dbo].[SuperShop]
WHERE YEAR([Order_Date]) = @YEAR
ORDER BY [Profit] DESC
GO
EXEC spTopProfitConsumerByYear 2017;
-------------------------------------------------------------------------------------
-- 11. Select Products by Discount Rate
USE SuperShopDB
GO
DROP PROCEDURE IF EXISTS spSelectProductsByDiscountRate
GO
CREATE PROCEDURE spSelectProductsByDiscountRate (@DISCOUNT DECIMAL)
AS
SELECT [Product_Name], [Discount]
FROM [dbo].[SuperShop]
WHERE [Discount] <= @DISCOUNT
ORDER BY [Discount] DESC
GO
EXEC spSelectProductsByDiscountRate 0.4;
-------------------------------------------------------------------------------------
-- 12. Total Customers by City
USE SuperShopDB
GO
SELECT [City], COUNT([Customer_ID]) AS 'Total Customers'
FROM [dbo].[SuperShop]
GROUP BY [City]
ORDER BY COUNT([Customer_ID]) DESC
-------------------------------------------------------------------------------------
-- 13. Total Customers by State
USE SuperShopDB
GO
SELECT [State], COUNT([Customer_ID]) AS 'Total Customers'
FROM [dbo].[SuperShop]
GROUP BY [State]
ORDER BY COUNT([Customer_ID]) DESC
-------------------------------------------------------------------------------------
-- THE END
-------------------------------------------------------------------------------------