-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter 1.sql
120 lines (98 loc) · 3.45 KB
/
Chapter 1.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
USE StockAnalysisDemo;
GO
--1-1.1 Using a subquery
SELECT TickerSymbol, TradeDate, ClosePrice,
(SELECT TOP(1) ClosePrice
FROM StockHistory AS SQ
WHERE SQ.TickerSymbol = OQ.TickerSymbol
AND SQ.TradeDate < OQ.TradeDate
ORDER BY TradeDate DESC) AS PrevClosePrice
FROM StockHistory AS OQ
ORDER BY TickerSymbol, TradeDate;
--1-1.2 Using LAG
SELECT TickerSymbol, TradeDate, ClosePrice,
LAG(ClosePrice) OVER(PARTITION BY TickerSymbol
ORDER BY TradeDate) AS PrevClosePrice
FROM StockHistory
ORDER BY TickerSymbol, TradeDate;
USE AdventureWorks;
GO
--1-2.1 Row numbers applied by CustomerID
SELECT CustomerID, SalesOrderID,
ROW_NUMBER() OVER(ORDER BY CustomerID) AS RowNumber
FROM Sales.SalesOrderHeader;
--1-2.2 Row numbers applied by SalesOrderID
SELECT CustomerID, SalesOrderID,
ROW_NUMBER() OVER(ORDER BY SalesOrderID) AS RowNumber
FROM Sales.SalesOrderHeader;
--1-3.1 Row number with a different ORDER BY
SELECT CustomerID, SalesOrderID,
ROW_NUMBER() OVER(ORDER BY CustomerID) AS RowNumber
FROM Sales.SalesOrderHeader
ORDER BY SalesOrderID;
--1-4.1 Row number with a descending ORDER BY
SELECT CustomerID, SalesOrderID,
ROW_NUMBER() OVER(ORDER BY CustomerID DESC) AS RowNumber
FROM Sales.SalesOrderHeader;
--1-5.1 Row number with a random ORDER BY
SELECT CustomerID, SalesOrderID,
ROW_NUMBER() OVER(ORDER BY NEWID()) AS RowNumber
FROM Sales.SalesOrderHeader;
--1-6.1 Use a constant for an ORDER BY
SELECT CustomerID, SalesOrderID,
ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS RowNumber
FROM Sales.SalesOrderHeader;
--1-6.2 Apply an ORDER BY to the query
SELECT CustomerID, SalesOrderID,
ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS RowNumber
FROM Sales.SalesOrderHeader
ORDER BY SalesOrderID;
--1-6.3 No ROW_NUMBER and no ORDER BY
SELECT CustomerID, SalesOrderID
FROM Sales.SalesOrderHeader;
--1-7.1 OVER clause has just CustomerID
SELECT CustomerID, SalesOrderID,
ROW_NUMBER() OVER(ORDER BY CustomerID) AS RowNumber
FROM Sales.SalesOrderHeader
ORDER BY CustomerID, SalesOrderID;
--1-7.2 Same query, just a new ORDER BY clause
SELECT CustomerID, SalesOrderID,
ROW_NUMBER() OVER(ORDER BY CustomerID) AS RowNumber
FROM Sales.SalesOrderHeader
ORDER BY CustomerID, SalesOrderID DESC;
--1-8.1 Use an expression in the ORDER BY
SELECT CustomerID, SalesOrderID, OrderDate,
ROW_NUMBER() OVER(ORDER BY CASE WHEN OrderDate > '2013/12/31'
THEN 0 ELSE 1 END, SalesOrderID) AS RowNumber
FROM Sales.SalesOrderHeader;
--1-9.1 Use ROW_NUMBER with PARTITION BY
SELECT CustomerID, SalesOrderID,
ROW_NUMBER() OVER(PARTITION BY CustomerID ORDER BY SalesOrderID)
AS RowNumber
FROM Sales.SalesOrderHeader;
--1-10.1 Using DISTINCT
SELECT DISTINCT OrderDate,
ROW_NUMBER() OVER(ORDER BY OrderDate) AS RowNumber
FROM Sales.SalesOrderHeader
ORDER BY RowNumber;
--1-10.2 Separate logic with CTE
WITH OrderDates AS (
SELECT DISTINCT OrderDate
FROM Sales.SalesOrderHeader)
SELECT OrderDate,
ROW_NUMBER() OVER(ORDER BY OrderDate) AS RowNumber
FROM OrderDates
ORDER BY RowNumber;
--1-11.1 Using TOP with ROW_NUMBER
SELECT TOP(6) CustomerID, SalesOrderID,
ROW_NUMBER() OVER(ORDER BY SalesOrderID) AS RowNumber
FROM Sales.SalesOrderHeader
ORDER BY NEWID();
--1-11.2 Separate the logic with a CTE
WITH Orders AS (
SELECT TOP(6) CustomerID, SalesOrderID
FROM Sales.SalesOrderHeader
ORDER BY NEWID())
SELECT CustomerID, SalesOrderID,
ROW_NUMBER() OVER(ORDER BY SalesOrderID) AS RowNumber
FROM Orders;