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
|
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, row_number() over () from t1;
pk row_number() over ()
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
explain FORMAT=JSON select pk, row_number() over () from t1;
EXPLAIN
{
"query_block": {
"select_id": 1,
"window_functions_computation": {
"sorts": {
"filesort": {
"sort_key": "pk"
}
},
"temporary_table": {
"table": {
"table_name": "t1",
"access_type": "index",
"key": "PRIMARY",
"key_length": "4",
"used_key_parts": ["pk"],
"rows": 11,
"filtered": 100,
"using_index": true
}
}
}
}
}
explain FORMAT=JSON select row_number() over (), pk from t1;
EXPLAIN
{
"query_block": {
"select_id": 1,
"window_functions_computation": {
"sorts": {
"filesort": {
"sort_key": "`row_number() over ()`"
}
},
"temporary_table": {
"table": {
"table_name": "t1",
"access_type": "index",
"key": "PRIMARY",
"key_length": "4",
"used_key_parts": ["pk"],
"rows": 11,
"filtered": 100,
"using_index": true
}
}
}
}
}
select row_number() over () from (select 4) as t;
row_number() over ()
1
select min(a) over (), max(a) over (), a, row_number() over ()
from t1
where a = 0;
min(a) over () max(a) over () a row_number() over ()
0 0 0 1
0 0 0 2
0 0 0 3
select a, min(a) over (), max(a) over (), row_number() over ()
from t1
where a = 0;
a min(a) over () max(a) over () row_number() over ()
0 0 0 1
0 0 0 2
0 0 0 3
select min(a) over () + 1, max(a) over (), row_number() over ()
from t1
where a = 0;
min(a) over () + 1 max(a) over () row_number() over ()
1 0 1
1 0 2
1 0 3
select min(a) over () + a, max(a) over (), row_number() over ()
from t1
where a = 1;
min(a) over () + a max(a) over () row_number() over ()
2 1 1
2 1 2
2 1 3
select a + min(a) over (), max(a) over (), row_number() over ()
from t1
where a = 1;
a + min(a) over () max(a) over () row_number() over ()
2 1 1
2 1 2
2 1 3
select a + min(a) over () from t1 where a = 1;
a + min(a) over ()
2
2
2
create view win_view
as (select a, min(a) over () from t1 where a = 1);
select * from win_view;
a min(a) over ()
1 1
1 1
1 1
drop view win_view;
create view win_view
as (select a, max(a + 1) over () from t1 where a = 1);
select * from win_view;
a max(a + 1) over ()
1 2
1 2
1 2
drop view win_view;
drop table t1;
|