-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart2 lag.sql
More file actions
60 lines (55 loc) · 1.99 KB
/
part2 lag.sql
File metadata and controls
60 lines (55 loc) · 1.99 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
SELECT custid, orderdate, orderid, val,
LAG(val) OVER(PARTITION BY custid ORDER BY orderdate, orderid) AS prevval,
LEAD(val) OVER(PARTITION BY custid ORDER BY orderdate, orderid) AS nextval
FROM Sales.OrderValues
SELECT custid, orderdate, orderid,
LAG(val, 3) OVER(PARTITION BY custid ORDER BY orderdate, orderid) AS prev3val
FROM Sales.OrderValues
WITH OrdersRN AS
(
SELECT custid, orderdate, orderid, val,
ROW_NUMBER() OVER(ORDER BY custid, orderdate, orderid) AS rn
FROM Sales.OrderValues
)
SELECT C.custid, C.orderdate, C.orderid, C.val,
P.val AS prevval,
N.val AS nextval
FROM OrdersRN AS C
LEFT OUTER JOIN OrdersRn AS P
ON C.custid = P.custid
AND C.rn = P.rn + 1
LEFT OUTER JOIN OrdersRn AS N
ON C.custid = N.custid
AND C.rn = N.rn - 1
SELECT custid, orderdate, orderid, val,
FIRST_VALUE(val) OVER(PARTITION BY custid ORDER BY orderdate, orderid) AS val_firstorder,
LAST_VALUE(val) OVER(PARTITION BY custid ORDER BY orderdate, orderid
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS val_lastorder
FROM Sales.OrderValues
SELECT custid, orderdate, orderid, val,
val - FIRST_VALUE(val) OVER(PARTITION BY custid ORDER BY orderdate, orderid) AS difffirst,
val - LAST_VALUE(val) OVER(PARTITION BY custid ORDER BY orderdate, orderid
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS difflast
FROM Sales.OrderValues
WITH OrdersRN AS
(
SELECT custid, val,
ROW_NUMBER() OVER(PARTITION BY custid ORDER BY orderdate, orderid) AS rna,
ROW_NUMBER() OVER(PARTITION BY custid ORDER BY orderdate DESC, orderid DESC) AS rnd
FROM Sales.OrderValues
),
Agg AS
(
SELECT custid,
MAX(CASE WHEN rna = 1 THEN val END) AS firstorderval,
MAX(CASE WHEN rnd = 1 THEN val END) AS lastorderval,
MAX(CASE WHEN rna = 3 THEN val END) AS thirdorderval
FROM OrdersRN
GROUP BY custid
)
SELECT O.custid, O.orderdate, O.orderid, O.val,
A.firstorderval, A.lastorderval, A.thirdorderval
FROM Sales.OrderValues AS O
INNER JOIN Agg AS A
ON O.custid = A.custid
ORDER BY custid, orderdate, orderid