-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL(Advanced)2.CTE'S.sql
More file actions
337 lines (293 loc) · 8.07 KB
/
Copy pathSQL(Advanced)2.CTE'S.sql
File metadata and controls
337 lines (293 loc) · 8.07 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
USE SalesDB
GO
---/* CTES🔆 */ ---
--🔅---NON-RECURSIVE CTES;
--🔅STANDALONE CTES In Action
/*🤖STEP 1 Task 1.1 : Finding the Total Sales per Customer */
---Stand-Alone CTE Build-Up
WITH CTE_Total_Sales AS (
SELECT
CustomerID
,SUM(Sales) [Total sales]
FROM Sales.Orders
GROUP BY CustomerID
)
---Main Query Model
SELECT
c.CustomerID
,CONCAT(FirstName , ' ', LastName) [Full Name]
,cts.[Total sales]
FROM Sales.Customers c
LEFT JOIN CTE_Total_Sales cts
ON c.CustomerID = cts.CustomerID
--🔅---NON-RECURSIVE CTES;
--🔅MULTIPLE STANDALONE CTES In Action
/*🤖STEP 2 Task 1.1 : Finding the Last Order-Date For Each Customer */
---Stand Alone CTE Build-Up
WITH CTE_Last_Order As
(
SELECT
CustomerID
,MAX(OrderDate) [Last Order Date]
FROM Sales.Orders
GROUP BY CustomerID
)
/*🤖STEP 3 Task 1.1 : Ranking Customers Based On Total Sales Per Customer */
---StandAlone CTE Build-Up
,CTE_Customer_Rank As
(
SELECT
CustomerID
,SUM(Sales) [Total Sales]
,RANK() OVER(ORDER BY SUM(Sales) DESC) [Customer Rank]
FROM Sales.Orders
GROUP BY CustomerID
)
---Main Query Model
SELECT
c.CustomerID
,CONCAT(FirstName , ' ', LastName) [Full Name]
,clo.[Last Order Date]
,ctr.[Customer Rank]
FROM Sales.Customers c
LEFT JOIN CTE_Last_Order clo
ON c.CustomerID = clo.CustomerID
LEFT JOIN CTE_Customer_Rank ctr
ON c.CustomerID = ctr.CustomerID
---StandAlone CTE BuildUp
WITH CTE_Total_Sales AS (
SELECT
CustomerID
,SUM(Sales) [Total sales]
FROM Sales.Orders
GROUP BY CustomerID
)
--🔅---NON-RECURSIVE CTES;
--🔅NESTED CTES In Action --A Type of CTE That works Leeching Off Another CTE.
/*🤖STEP 4 Task 1.1 : Segmenting Customers Based on Their Total Sales */
---Nested CTE Build-Up
,CTE_Customer_Segment As
(
SELECT
CustomerID
,[Total Sales]
,CASE WHEN [Total Sales] > 100
THEN 'High'
WHEN [Total Sales] > 80
THEN 'Medium'
ELSE 'Low'
END Cst_Segment
FROM CTE_Total_Sales
)
---Main Query Model
SELECT
c.CustomerID
,CONCAT(FirstName , ' ', LastName) [Full Name]
,cts.[Total sales]
,ccs.Cst_Segment
FROM Sales.Customers c
LEFT JOIN CTE_Total_Sales cts
ON c.CustomerID = cts.CustomerID
LEFT JOIN CTE_Customer_Segment ccs
ON c.CustomerID = ccs.CustomerID
---🔅RECURSIVE CTES In Action --Much Like A loop : It queries repeatedly until a specific conditon is met
---Can Be Used In HIERARCHY Structures
/*Task 1.1 : Generating a Sequence Of Numbers From 1 to 20 */
WITH Series As
(
---Anchor Query
SELECT 1 As My_Number
UNION ALL
--- Recursive CTE query
SELECT My_Number +1
FROM Series
WHERE My_Number < 200
)
---Main Query
SELECT *
FROM Series
OPTION (MAXRECURSION 1000) --To Ctrl The Recursion LIMITATION Of 100
/*Task 1.1 : Showing The Employee Hierarchy By Displaying
Each Employees Level Within The Organisation */
---Recursive Query Build-Up
WITH CTE_Employee_Hierarchy As
(
-- Anchor Query
SELECT
[EmployeeID]
,[FirstName]
,[ManagerID] ,
1 As Level
FROM Sales.Employees
WHERE ManagerID IS NULL
UNION ALL
---Recursive CTE_Query
SELECT
e.[EmployeeID]
,e.[FirstName]
,e.[ManagerID]
,ceh.Level + 1
FROM Sales.Employees e
INNER JOIN CTE_Employee_Hierarchy ceh
ON e.ManagerID = ceh.EmployeeID
)
---Main Query Model
SELECT *
FROM CTE_Employee_Hierarchy
--🔅---VIEWS ;
--🔅VIEWS In Action
---#1 USAGE: #Reducing Redundancies
---##TASK:Finding the Running Total Of Sales For Each Month
---🤖 Step 1:
/* WITH CTE_Monthly_Summary as
(
---Anchor CTE Query Build-Up
SELECT
DATETRUNC(MONTH , OrderDate) [Order Month]
,SUM(Sales) [Total Sales]
,COUNT(OrderID) [Total Orders]
,SUM(Quantity) [Total Quantity]
FROM Sales.Orders
GROUP BY DATETRUNC(MONTH , OrderDate)
)
---MAIN QUERY MODEL
SELECT
[Order Month]
,[Total Quantity]
,[Total Sales]
,SUM([Total Sales]) OVER (ORDER BY [Order Month]) [Running Total]
FROM CTE_Monthly_Summary
*/
---🤖 Step 2: pUtting the CTE Query In VIEW
CREATE VIEW Sales.CTE_Monthly_Summary As
(
---VIEW Query Build-Up
SELECT
DATETRUNC(MONTH , OrderDate) [Order Month]
,SUM(Sales) [Total Sales]
,COUNT(OrderID) [Total Orders]
,SUM(Quantity) [Total Quantity]
FROM Sales.Orders
GROUP BY DATETRUNC(MONTH , OrderDate)
)
---🤖 Step 3: pUtting the View Query To Work
---MAIN QUERY MODEL
SELECT
[Order Month]
,[Total Quantity]
,[Total Sales]
,SUM([Total Sales]) OVER (ORDER BY [Order Month]) [Running Total]
FROM Sales.CTE_Monthly_Summary
---🤖 Step 4: Make Sure the View Query Is in Right SCHEMA
---🤖 Step 5: Removing Unnecessary VIEWS
DROP VIEW dbo.CTE_Monthly_Summary
---🤖 Step 6: Updating An EXISTING VIEW ;Adding Or Removing
--DROP VIEW
--Then Recreate THE VIEW With UNeccessary Changes
----OR T-SQL
IF OBJECT_ID('Sales.Monthly_Summary','V') IS NOT NULL
DROP VIEW Sales.Monthly_Summary;
GO
CREATE VIEW Sales.Monthly_Summary As
(
SELECT
DATETRUNC(MONTH , OrderDate) [Order Month]
,SUM(Sales) [Total Sales]
,COUNT(OrderID) [Total Orders]
FROM Sales.Orders
GROUP BY DATETRUNC(MONTH , OrderDate)
)
---#2 USAGE: #Reducing/Hiding Complexities of DB Tables
---##TASK:Providing A View That Combines Details From ORDERS ,PRODUCTS, CUSTOMER, And EMPLOYEES
---🤖 Step 1: Query Build-Up to Use In The View
SELECT
o.OrderID
,o.OrderDate
,p.Product
,p.Category
,COALESCE(c.FirstName, ' ') + ' ' +COALESCE(c.LastName, ' ') [Customer Name]
,c.Country [Customer Country]
,COALESCE(e.FirstName, ' ') + ' ' +COALESCE(e.LastName, ' ') [Employees Name]
,e.Department
,o.Sales ,o.Quantity
FROM Sales.Orders o
LEFT JOIN Sales.Products p
ON p.ProductID = o.ProductID
LEFT JOIN Sales.Customers c
ON c.CustomerID = o.CustomerID
LEFT JOIN Sales.Employees e
ON e.EmployeeID = o.SalesPersonID
---🤖 Step 2: Putting The Query In View Model
CREATE VIEW Sales.View_Order_Details As
(
SELECT
o.OrderID
,o.OrderDate
,p.Product
,p.Category
,COALESCE(c.FirstName, ' ') + ' ' +COALESCE(c.LastName, ' ') [Customer Name]
,c.Country [Customer Country]
,COALESCE(e.FirstName, ' ') + ' ' +COALESCE(e.LastName, ' ') [Employees Name]
,e.Department
,o.Sales ,o.Quantity
FROM Sales.Orders o
LEFT JOIN Sales.Products p
ON p.ProductID = o.ProductID
LEFT JOIN Sales.Customers c
ON c.CustomerID = o.CustomerID
LEFT JOIN Sales.Employees e
ON e.EmployeeID = o.SalesPersonID
)
---🤖 Step 3: Putting The Query To Work
SELECT
OrderID
,OrderDate
,[Customer Name]
,[Customer Country]
,Sales
FROM Sales.View_Order_Details
---#3 USAGE: #Data Security/Security of DB Tables
---##TASK:Providing A View That Combines Details From All Tables AND Excludes Data Related To the USA
---🤖 Step 1: Query Build-Up to Use In The View
SELECT
o.OrderID
,o.OrderDate
,p.Product
,p.Category
,COALESCE(c.FirstName, ' ') + ' ' +COALESCE(c.LastName, ' ') [Customer Name]
,c.Country [Customer Country]
,COALESCE(e.FirstName, ' ') + ' ' +COALESCE(e.LastName, ' ') [Employees Name]
,e.Department
,o.Sales ,o.Quantity
FROM Sales.Orders o
LEFT JOIN Sales.Products p
ON p.ProductID = o.ProductID
LEFT JOIN Sales.Customers c
ON c.CustomerID = o.CustomerID
LEFT JOIN Sales.Employees e
ON e.EmployeeID = o.SalesPersonID
---🤖 Step 2: Putting Query In View Model
CREATE VIEW Sales.All_Orders_Details_EU As
(
SELECT
o.OrderID
,o.OrderDate
,p.Product
,p.Category
,COALESCE(c.FirstName, ' ') + ' ' +COALESCE(c.LastName, ' ') [Customer Name]
,c.Country [Customer Country]
,COALESCE(e.FirstName, ' ') + ' ' +COALESCE(e.LastName, ' ') [Employees Name]
,e.Department
,o.Sales ,o.Quantity
FROM Sales.Orders o
LEFT JOIN Sales.Products p
ON p.ProductID = o.ProductID
LEFT JOIN Sales.Customers c
ON c.CustomerID = o.CustomerID
LEFT JOIN Sales.Employees e
ON e.EmployeeID = o.SalesPersonID
WHERE c.Country != 'USA'
)
---🤖 Step 3: Putting The Query To Work
SELECT *
FROM Sales.All_Orders_Details_EU