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
|
create table t1 (
pk int primary key,
a int,
b int,
c char(10),
d decimal(10, 3),
e real
);
insert into t1 values
( 1, 0, 1, 'one', 0.1, 0.001),
( 2, 0, 2, 'two', 0.2, 0.002),
( 3, 0, 3, 'three', 0.3, 0.003),
( 4, 1, 2, 'three', 0.4, 0.004),
( 5, 1, 1, 'two', 0.5, 0.005),
( 6, 1, 1, 'one', 0.6, 0.006),
( 7, 2, NULL, 'n_one', 0.5, 0.007),
( 8, 2, 1, 'n_two', NULL, 0.008),
( 9, 2, 2, NULL, 0.7, 0.009),
(10, 2, 0, 'n_four', 0.8, 0.010),
(11, 2, 10, NULL, 0.9, NULL);
select pk,
lead(pk) over (order by pk),
lead(pk, 1) over (order by pk),
lead(pk, 2) over (order by pk),
lead(pk, 0) over (order by pk),
lead(pk, -1) over (order by pk),
lead(pk, -2) over (order by pk)
from t1
order by pk asc;
pk lead(pk) over (order by pk) lead(pk, 1) over (order by pk) lead(pk, 2) over (order by pk) lead(pk, 0) over (order by pk) lead(pk, -1) over (order by pk) lead(pk, -2) over (order by pk)
1 2 2 3 1 NULL NULL
2 3 3 4 2 1 NULL
3 4 4 5 3 2 1
4 5 5 6 4 3 2
5 6 6 7 5 4 3
6 7 7 8 6 5 4
7 8 8 9 7 6 5
8 9 9 10 8 7 6
9 10 10 11 9 8 7
10 11 11 NULL 10 9 8
11 NULL NULL NULL 11 10 9
select pk,
lag(pk) over (order by pk),
lag(pk, 1) over (order by pk),
lag(pk, 2) over (order by pk),
lag(pk, 0) over (order by pk),
lag(pk, -1) over (order by pk),
lag(pk, -2) over (order by pk)
from t1
order by pk asc;
pk lag(pk) over (order by pk) lag(pk, 1) over (order by pk) lag(pk, 2) over (order by pk) lag(pk, 0) over (order by pk) lag(pk, -1) over (order by pk) lag(pk, -2) over (order by pk)
1 NULL NULL NULL 1 2 3
2 1 1 NULL 2 3 4
3 2 2 1 3 4 5
4 3 3 2 4 5 6
5 4 4 3 5 6 7
6 5 5 4 6 7 8
7 6 6 5 7 8 9
8 7 7 6 8 9 10
9 8 8 7 9 10 11
10 9 9 8 10 11 NULL
11 10 10 9 11 NULL NULL
select pk, pk - 2,
lag(pk, pk - 2) over (order by pk),
lead(pk, pk - 2) over (order by pk)
from t1
order by pk asc;
pk pk - 2 lag(pk, pk - 2) over (order by pk) lead(pk, pk - 2) over (order by pk)
1 -1 2 NULL
2 0 2 2
3 1 2 4
4 2 2 6
5 3 2 8
6 4 2 10
7 5 2 NULL
8 6 2 NULL
9 7 2 NULL
10 8 2 NULL
11 9 2 NULL
select pk, pk - 2,
lag(pk, pk + 2) over (order by pk),
lead(pk, pk + 2) over (order by pk)
from t1
order by pk asc;
pk pk - 2 lag(pk, pk + 2) over (order by pk) lead(pk, pk + 2) over (order by pk)
1 -1 NULL 4
2 0 NULL 6
3 1 NULL 8
4 2 NULL 10
5 3 NULL NULL
6 4 NULL NULL
7 5 NULL NULL
8 6 NULL NULL
9 7 NULL NULL
10 8 NULL NULL
11 9 NULL NULL
select pk, a,
lead(pk) over (partition by a order by pk),
lead(pk, 1) over (partition by a order by pk),
lead(pk, 2) over (partition by a order by pk),
lead(pk, 0) over (partition by a order by pk),
lead(pk, -1) over (partition by a order by pk),
lead(pk, -2) over (partition by a order by pk)
from t1
order by pk asc;
pk a lead(pk) over (partition by a order by pk) lead(pk, 1) over (partition by a order by pk) lead(pk, 2) over (partition by a order by pk) lead(pk, 0) over (partition by a order by pk) lead(pk, -1) over (partition by a order by pk) lead(pk, -2) over (partition by a order by pk)
1 0 2 2 3 1 NULL NULL
2 0 3 3 NULL 2 1 NULL
3 0 NULL NULL NULL 3 2 1
4 1 5 5 6 4 NULL NULL
5 1 6 6 NULL 5 4 NULL
6 1 NULL NULL NULL 6 5 4
7 2 8 8 9 7 NULL NULL
8 2 9 9 10 8 7 NULL
9 2 10 10 11 9 8 7
10 2 11 11 NULL 10 9 8
11 2 NULL NULL NULL 11 10 9
select pk, a,
lag(pk) over (partition by a order by pk),
lag(pk, 1) over (partition by a order by pk),
lag(pk, 2) over (partition by a order by pk),
lag(pk, 0) over (partition by a order by pk),
lag(pk, -1) over (partition by a order by pk),
lag(pk, -2) over (partition by a order by pk)
from t1
order by pk asc;
pk a lag(pk) over (partition by a order by pk) lag(pk, 1) over (partition by a order by pk) lag(pk, 2) over (partition by a order by pk) lag(pk, 0) over (partition by a order by pk) lag(pk, -1) over (partition by a order by pk) lag(pk, -2) over (partition by a order by pk)
1 0 NULL NULL NULL 1 2 3
2 0 1 1 NULL 2 3 NULL
3 0 2 2 1 3 NULL NULL
4 1 NULL NULL NULL 4 5 6
5 1 4 4 NULL 5 6 NULL
6 1 5 5 4 6 NULL NULL
7 2 NULL NULL NULL 7 8 9
8 2 7 7 NULL 8 9 10
9 2 8 8 7 9 10 11
10 2 9 9 8 10 11 NULL
11 2 10 10 9 11 NULL NULL
select pk, a, pk - 2,
lag(pk, pk - 2) over (partition by a order by pk),
lead(pk, pk - 2) over (partition by a order by pk),
lag(pk, a - 2) over (partition by a order by pk),
lead(pk, a - 2) over (partition by a order by pk)
from t1
order by pk asc;
pk a pk - 2 lag(pk, pk - 2) over (partition by a order by pk) lead(pk, pk - 2) over (partition by a order by pk) lag(pk, a - 2) over (partition by a order by pk) lead(pk, a - 2) over (partition by a order by pk)
1 0 -1 2 NULL 3 NULL
2 0 0 2 2 NULL NULL
3 0 1 2 NULL NULL 1
4 1 2 NULL 6 5 NULL
5 1 3 NULL NULL 6 4
6 1 4 NULL NULL NULL 5
7 2 5 NULL NULL 7 7
8 2 6 NULL NULL 8 8
9 2 7 NULL NULL 9 9
10 2 8 NULL NULL 10 10
11 2 9 NULL NULL 11 11
select pk, a, pk - 2,
lag(pk, pk + 2) over (partition by a order by pk),
lead(pk, pk + 2) over (partition by a order by pk),
lag(pk, a + 2) over (partition by a order by pk),
lead(pk, a + 2) over (partition by a order by pk)
from t1
order by pk asc;
pk a pk - 2 lag(pk, pk + 2) over (partition by a order by pk) lead(pk, pk + 2) over (partition by a order by pk) lag(pk, a + 2) over (partition by a order by pk) lead(pk, a + 2) over (partition by a order by pk)
1 0 -1 NULL NULL NULL 3
2 0 0 NULL NULL NULL NULL
3 0 1 NULL NULL 1 NULL
4 1 2 NULL NULL NULL NULL
5 1 3 NULL NULL NULL NULL
6 1 4 NULL NULL NULL NULL
7 2 5 NULL NULL NULL 11
8 2 6 NULL NULL NULL NULL
9 2 7 NULL NULL NULL NULL
10 2 8 NULL NULL NULL NULL
11 2 9 NULL NULL 7 NULL
select pk, a, b, c, d, e,
lag(a) over (partition by a order by pk),
lag(b) over (partition by a order by pk),
lag(c) over (partition by a order by pk),
lag(d) over (partition by a order by pk),
lag(e) over (partition by a order by pk)
from t1
order by pk asc;
pk a b c d e lag(a) over (partition by a order by pk) lag(b) over (partition by a order by pk) lag(c) over (partition by a order by pk) lag(d) over (partition by a order by pk) lag(e) over (partition by a order by pk)
1 0 1 one 0.100 0.001 NULL NULL NULL NULL NULL
2 0 2 two 0.200 0.002 0 1 one 0.100 0.001
3 0 3 three 0.300 0.003 0 2 two 0.200 0.002
4 1 2 three 0.400 0.004 NULL NULL NULL NULL NULL
5 1 1 two 0.500 0.005 1 2 three 0.400 0.004
6 1 1 one 0.600 0.006 1 1 two 0.500 0.005
7 2 NULL n_one 0.500 0.007 NULL NULL NULL NULL NULL
8 2 1 n_two NULL 0.008 2 NULL n_one 0.500 0.007
9 2 2 NULL 0.700 0.009 2 1 n_two NULL 0.008
10 2 0 n_four 0.800 0.01 2 2 NULL 0.700 0.009
11 2 10 NULL 0.900 NULL 2 0 n_four 0.800 0.01
select pk, a, b, a+b,
lag(a + b) over (partition by a order by pk)
from t1
order by pk asc;
pk a b a+b lag(a + b) over (partition by a order by pk)
1 0 1 1 NULL
2 0 2 2 1
3 0 3 3 2
4 1 2 3 NULL
5 1 1 2 3
6 1 1 2 2
7 2 NULL NULL NULL
8 2 1 3 NULL
9 2 2 4 3
10 2 0 2 4
11 2 10 12 2
select pk, a, b, a+b,
lag(a + b) over (partition by a order by pk) + pk
from t1
order by pk asc;
pk a b a+b lag(a + b) over (partition by a order by pk) + pk
1 0 1 1 NULL
2 0 2 2 3
3 0 3 3 5
4 1 2 3 NULL
5 1 1 2 8
6 1 1 2 8
7 2 NULL NULL NULL
8 2 1 3 NULL
9 2 2 4 12
10 2 0 2 14
11 2 10 12 13
#
# MDEV-15204 - LAG function doesn't require ORDER BY in OVER clause
#
select pk,
lag(pk, 1) over ()
from t1;
ERROR HY000: No order list in window specification for 'lag'
select pk,
lead(pk, 1) over ()
from t1;
ERROR HY000: No order list in window specification for 'lead'
drop table t1;
|