2020-12-18 14:34:22 +00:00
|
|
|
set allow_experimental_window_functions = 1;
|
|
|
|
|
|
|
|
-- just something basic
|
|
|
|
|
2020-12-18 00:49:18 +00:00
|
|
|
select number, count() over (partition by intDiv(number, 3) order by number) from numbers(10);
|
|
|
|
|
|
|
|
-- proper calculation across blocks
|
|
|
|
|
|
|
|
0 1
|
|
|
|
1 2
|
|
|
|
2 3
|
|
|
|
3 1
|
|
|
|
4 2
|
|
|
|
5 3
|
|
|
|
6 1
|
|
|
|
7 2
|
|
|
|
8 3
|
|
|
|
9 1
|
|
|
|
select number, max(number) over (partition by intDiv(number, 3) order by number desc) from numbers(10) settings max_block_size = 2;
|
|
|
|
|
|
|
|
-- not a window function
|
|
|
|
|
|
|
|
2 2
|
|
|
|
1 2
|
|
|
|
0 2
|
|
|
|
5 5
|
|
|
|
4 5
|
|
|
|
3 5
|
|
|
|
8 8
|
|
|
|
7 8
|
|
|
|
6 8
|
|
|
|
9 9
|
|
|
|
select number, abs(number) over (partition by toString(intDiv(number, 3))) from numbers(10); -- { serverError 63 }
|
|
|
|
|
|
|
|
-- no partition by
|
|
|
|
|
|
|
|
select number, avg(number) over (order by number) from numbers(10);
|
|
|
|
|
|
|
|
-- no order by
|
|
|
|
|
|
|
|
0 0
|
|
|
|
1 0
|
|
|
|
2 1
|
|
|
|
3 1
|
|
|
|
4 2
|
|
|
|
5 2
|
|
|
|
6 3
|
|
|
|
7 3
|
|
|
|
8 4
|
|
|
|
9 4
|
|
|
|
select number, quantileExact(number) over (partition by intDiv(number, 3)) from numbers(10);
|
|
|
|
|
|
|
|
-- can add an alias after window spec
|
|
|
|
|
|
|
|
0 0
|
|
|
|
1 1
|
|
|
|
2 1
|
|
|
|
3 3
|
|
|
|
4 4
|
|
|
|
5 4
|
|
|
|
6 6
|
|
|
|
7 7
|
|
|
|
8 7
|
|
|
|
9 9
|
|
|
|
select number, quantileExact(number) over (partition by intDiv(number, 3)) q from numbers(10);
|
|
|
|
|
|
|
|
-- can't reference it yet -- the window functions are calculated at the
|
|
|
|
-- last stage of select, after all other functions.
|
|
|
|
|
|
|
|
0 0
|
|
|
|
1 1
|
|
|
|
2 1
|
|
|
|
3 3
|
|
|
|
4 4
|
|
|
|
5 4
|
|
|
|
6 6
|
|
|
|
7 7
|
|
|
|
8 7
|
|
|
|
9 9
|
|
|
|
select q * 10, quantileExact(number) over (partition by intDiv(number, 3)) q from numbers(10); -- { serverError 47 }
|
|
|
|
|
|
|
|
-- should work in ORDER BY though
|
|
|
|
|
|
|
|
select number, max(number) over (partition by intDiv(number, 3) order by number desc) m from numbers(10) order by m desc, number;
|
|
|
|
|
|
|
|
|
|
|
|
9 9
|
|
|
|
6 8
|
|
|
|
7 8
|
|
|
|
8 8
|
|
|
|
3 5
|
|
|
|
4 5
|
|
|
|
5 5
|
|
|
|
0 2
|
|
|
|
1 2
|
|
|
|
2 2
|