-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter 8.sql
257 lines (209 loc) · 6.69 KB
/
Chapter 8.sql
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
--8-1.1 A simple query
SELECT *
FROM HumanResources.Employee;
--8-2.1 Query to produce Sequence Project (Compute Scalar) operator
SELECT CustomerID, ROW_NUMBER() OVER(ORDER BY SalesOrderID) AS RowNumber
FROM Sales.SalesOrderHeader;
--8-3.1 Add PARTITION BY
SELECT CustomerID,
ROW_NUMBER() OVER(PARTITION BY CustomerID ORDER BY SalesOrderID) AS RowNumber
FROM Sales.SalesOrderHeader;
--8-4.1 A query to show the Sort operator
SELECT CustomerID, SalesOrderID,
ROW_NUMBER() OVER(PARTITION BY CustomerID ORDER BY OrderDate) AS RowNumber
FROM Sales.SalesOrderHeader;
--8-5.1 A query with a Table Spool operator
SELECT CustomerID, SalesOrderID, SUM(TotalDue)
OVER(PARTITION BY CustomerID) AS SubTotal
FROM Sales.SalesOrderHeader;
--8-6.1 A query isth a window spool operator
SELECT CustomerID, SalesOrderID, TotalDue,
SUM(TotalDue) OVER(PARTITION BY CustomerID ORDER BY SalesOrderID) AS RunningTotal
FROM Sales.SalesOrderHeader;
--8-7.0 Settings
USE [master];
GO
--Change database name as needed
ALTER DATABASE [AdventureWorks2017]
SET COMPATIBILITY_LEVEL = 120;
GO
USE [AdventureWorks2017];
SET STATISTICS IO ON;
SET NOCOUNT ON;
GO
--8-7.1 Query to produce Sequence Project
PRINT '8-7.1';
SELECT CustomerID, ROW_NUMBER() OVER(ORDER BY SalesOrderID) AS RowNumber
FROM Sales.SalesOrderHeader;
--8-7.2 A query to show the Sort operator
PRINT '8-7.2';
SELECT CustomerID, SalesOrderID,
ROW_NUMBER() OVER(PARTITION BY CustomerID ORDER BY OrderDate) AS RowNumber
FROM Sales.SalesOrderHeader;
--8-7.3 A query with a Table Spool operator
PRINT '8-7.3';
SELECT CustomerID, SalesOrderID, SUM(TotalDue) OVER(PARTITION BY CustomerID)
AS SubTotal
FROM Sales.SalesOrderHeader;
--8-8.1 Drop the existing index
DROP INDEX [IX_SalesOrderHeader_CustomerID] ON [Sales].[SalesOrderHeader];
GO
--8-8.2 Create a new index for the query
CREATE NONCLUSTERED INDEX [IX_SalesOrderHeader_CustomerID_OrderDate]
ON [Sales].[SalesOrderHeader] ([CustomerID], [OrderDate]);
--8-9.1 query with a join
SELECT SOH.CustomerID, SOH.SalesOrderID, SOH.OrderDate, C.TerritoryID,
ROW_NUMBER() OVER(PARTITION BY SOH.CustomerID ORDER BY SOH.OrderDate)
AS RowNumber
FROM Sales.SalesOrderHeader AS SOH
JOIN Sales.Customer C ON SOH.CustomerID = C.CustomerID;
--8-9.2 Rearrange the query
WITH Sales AS (
SELECT CustomerID, OrderDate, SalesOrderID,
ROW_NUMBER() OVER(PARTITION BY CustomerID ORDER BY OrderDate)
AS RowNumber
FROM Sales.SalesOrderHeader)
SELECT Sales.CustomerID, SALES.SalesOrderID, Sales.OrderDate,
C.TerritoryID, Sales.RowNumber
FROM Sales
JOIN Sales.Customer AS C ON C.CustomerID = Sales.CustomerID;
--8-10.1 Set the compatibility level
USE master;
GO
ALTER DATABASE AdventureWorks2017
SET COMPATIBILITY_LEVEL = 140 WITH NO_WAIT;
GO
USE AdventureWorks2017;
GO
--8-10.2 Turn on Statistics IO
SET STATISTICS IO ON;
SET NOCOUNT ON;
GO
--8-10.3 Create a larger table for testing
DROP TABLE IF EXISTS dbo.SOD ;
CREATE TABLE dbo.SOD(SalesOrderID INT, SalesOrderDetailID INT, LineTotal Money);
--8-10.4 Populate the table
INSERT INTO dbo.SOD(SalesOrderID, SalesOrderDetailID, LineTotal)
SELECT SalesOrderID, SalesOrderDetailID, LineTotal
FROM Sales.SalesOrderDetail
UNION ALL
SELECT SalesOrderID + MAX(SalesOrderID) OVER(), SalesOrderDetailID, LineTotal
FROM Sales.SalesOrderDetail;
--8-10.5 Create a nonclustered index
CREATE INDEX SalesOrderID_SOD ON dbo.SOD
(SalesOrderID, SalesOrderDetailID) INCLUDE(LineTotal);
--8-11.1 A running total
PRINT '8-11.1'
SELECT SalesOrderID, SalesOrderDetailID, LineTotal,
SUM(LineTotal)
OVER(PARTITION BY SalesOrderID ORDER BY SalesOrderDetailID) RunningTotal
FROM SOD;
--8-11.2 A query with FIRST_VALUE
PRINT '8-11.2'
SELECT SalesOrderID, SalesOrderDetailID, LineTotal,
FIRST_VALUE(LineTotal)
OVER(PARTITION BY SalesOrderID ORDER BY SalesOrderDetailID) FirstValue
FROM SOD;
PRINT '8-12.1'
SELECT SalesOrderID, SalesOrderDetailID, LineTotal,
SUM(LineTotal)
OVER(PARTITION BY SalesOrderID ORDER BY SalesOrderDetailID
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) RunningTotal
FROM SOD;
--8-12.2 A query with FIRST_VALUE using ROWS
PRINT '8-12.2'
SELECT SalesOrderID, SalesOrderDetailID, LineTotal,
FIRST_VALUE(LineTotal)
OVER(PARTITION BY SalesOrderID ORDER BY SalesOrderDetailID
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) RunningTotal
FROM SOD;
--8-13.1 Set the compatibility level
USE master;
GO
ALTER DATABASE AdventureWorks2017
SET COMPATIBILITY_LEVEL = 140 WITH NO_WAIT;
GO
USE AdventureWorks2017;
GO
--8-13.2 A window aggregate
PRINT '8-13.1'
SELECT SalesOrderID, SalesOrderDetailID, LineTotal,
SUM(LineTotal) OVER(PARTITION BY SalesOrderID) AS SubTotal
FROM SOD;
--8-13.2 A statistical function
PRINT '8-11.2'
SELECT SalesOrderID, SalesOrderDetailID, LineTotal,
PERCENT_RANK()
OVER(PARTITION BY SalesOrderID ORDER BY SalesOrderDetailID) AS Ranking
FROM SOD;
--8-14.1 Set the compatibility level
USE master;
GO
ALTER DATABASE AdventureWorks2017
SET COMPATIBILITY_LEVEL = 150 WITH NO_WAIT;
GO
USE AdventureWorks2017;
GO
--8-14.2 A window aggregate
PRINT '8-14.1'
SELECT SalesOrderID, SalesOrderDetailID, LineTotal,
SUM(LineTotal) OVER(PARTITION BY SalesOrderID) AS SubTotal
FROM SOD;
--8-15.1 Change settings
SET STATISTICS IO OFF;
SET STATISTICS TIME ON;
SET NOCOUNT ON;
GO
--8-15.2 Change compatability
USE MASTER;
GO
ALTER DATABASE AdventureWorks2017
SET COMPATIBILITY_LEVEL = 140 WITH NO_WAIT;
USE AdventureWorks2017;
GO
--8-15.3
PRINT '
DEFAULT frame'
SELECT SalesOrderID, SalesOrderDetailID, LineTotal,
SUM(LineTotal) OVER(PARTITION BY SalesOrderID
ORDER BY SalesOrderDetailID) AS RunningTotal
FROM SOD;
--8-15.4
PRINT '
ROWS'
SELECT SalesOrderID, SalesOrderDetailID, LineTotal,
SUM(LineTotal) OVER(PARTITION BY SalesOrderID
ORDER BY SalesOrderDetailID
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS SubTotal
FROM SOD;
--8-16.1
PRINT '
DEFAULT frame'
SELECT SalesOrderID, SalesOrderDetailID, LineTotal,
SUM(LineTotal) OVER(PARTITION BY SalesOrderID
ORDER BY SalesOrderDetailID) AS RunningTotal
INTO #temp1
FROM SOD;
--8-16.2
PRINT '
ROWS'
SELECT SalesOrderID, SalesOrderDetailID, LineTotal,
SUM(LineTotal) OVER(PARTITION BY SalesOrderID
ORDER BY SalesOrderDetailID
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS SubTotal
INTO #Temp2
FROM SOD;
DROP TABLE #Temp1;
DROP TABLE #temp2;
--8-17.1 Drop index
DROP INDEX [IX_SalesOrderHeader_CustomerID_OrderDate]
ON Sales.SalesOrderHeader;
GO
--8-17-2 Recreate original index
CREATE INDEX [IX_SalesOrderHeader_CustomerID] ON Sales.SalesOrderHeader
(CustomerID);
--8-17-3 Drop special table
DROP TABLE IF EXISTS dbo.SOD;
--8-17-4 Drop Thinking Big Adventure tables
DROP TABLE IF EXISTS dbo.bigTransactionHistory;
DROP TABLE IF EXISTS dbo.bigProduct;