mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-21 15:12:02 +00:00
458 lines
7.9 KiB
Plaintext
458 lines
7.9 KiB
Plaintext
-- { echo }
|
|
|
|
set allow_experimental_window_functions = 1;
|
|
-- just something basic
|
|
select number, count() over (partition by intDiv(number, 3) order by number) from numbers(10);
|
|
0 1
|
|
1 2
|
|
2 3
|
|
3 1
|
|
4 2
|
|
5 3
|
|
6 1
|
|
7 2
|
|
8 3
|
|
9 1
|
|
-- proper calculation across blocks
|
|
select number, max(number) over (partition by intDiv(number, 3) order by number desc) from numbers(10) settings max_block_size = 2;
|
|
2 2
|
|
1 2
|
|
0 2
|
|
5 5
|
|
4 4
|
|
3 4
|
|
8 8
|
|
7 8
|
|
6 8
|
|
9 9
|
|
-- not a window function
|
|
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);
|
|
0 0
|
|
1 0.5
|
|
2 1
|
|
3 1.5
|
|
4 2
|
|
5 2.5
|
|
6 3
|
|
7 3.5
|
|
8 4
|
|
9 4.5
|
|
-- no order by
|
|
select number, quantileExact(number) over (partition by intDiv(number, 3)) from numbers(10);
|
|
0 0
|
|
1 1
|
|
2 1
|
|
3 3
|
|
4 4
|
|
5 4
|
|
6 6
|
|
7 7
|
|
8 7
|
|
9 9
|
|
-- can add an alias after window spec
|
|
select number, quantileExact(number) over (partition by intDiv(number, 3)) q from numbers(10);
|
|
0 0
|
|
1 1
|
|
2 1
|
|
3 3
|
|
4 4
|
|
5 4
|
|
6 6
|
|
7 7
|
|
8 7
|
|
9 9
|
|
-- can't reference it yet -- the window functions are calculated at the
|
|
-- last stage of select, after all other functions.
|
|
select q * 10, quantileExact(number) over (partition by intDiv(number, 3)) q from numbers(10); -- { serverError 47 }
|
|
-- must work in WHERE if you wrap it in a subquery
|
|
select * from (select count(*) over () c from numbers(3)) where c > 0;
|
|
1
|
|
2
|
|
3
|
|
-- should work in ORDER BY
|
|
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
|
|
-- also works in ORDER BY if you wrap it in a subquery
|
|
select * from (select count(*) over () c from numbers(3)) order by c;
|
|
1
|
|
2
|
|
3
|
|
-- Example with window function only in ORDER BY. Here we make a rank of all
|
|
-- numbers sorted descending, and then sort by this rank descending, and must get
|
|
-- the ascending order.
|
|
select * from (select * from numbers(5) order by rand()) order by count() over (order by number desc) desc;
|
|
0
|
|
1
|
|
2
|
|
3
|
|
4
|
|
-- Aggregate functions as window function arguments. This query is semantically
|
|
-- the same as the above one, only we replace `number` with
|
|
-- `any(number) group by number` and so on.
|
|
select * from (select * from numbers(5) order by rand()) group by number order by sum(any(number + 1)) over (order by min(number) desc) desc;
|
|
0
|
|
1
|
|
2
|
|
3
|
|
4
|
|
-- some more simple cases w/aggregate functions
|
|
select sum(any(number)) over () from numbers(1);
|
|
0
|
|
select sum(any(number) + 1) over () from numbers(1);
|
|
1
|
|
select sum(any(number + 1)) over () from numbers(1);
|
|
1
|
|
-- different windows
|
|
-- an explain test would also be helpful, but it's too immature now and I don't
|
|
-- want to change reference all the time
|
|
select number, max(number) over (partition by intDiv(number, 3) order by number desc), count(number) over (partition by intDiv(number, 5) order by number) as m from numbers(31) order by number settings max_block_size = 2;
|
|
0 2 1
|
|
1 2 2
|
|
2 2 3
|
|
3 4 4
|
|
4 4 5
|
|
5 5 1
|
|
6 8 1
|
|
7 8 2
|
|
8 8 3
|
|
9 10 4
|
|
10 10 1
|
|
11 11 2
|
|
12 14 3
|
|
13 14 4
|
|
14 14 5
|
|
15 16 1
|
|
16 16 1
|
|
17 17 2
|
|
18 20 3
|
|
19 20 4
|
|
20 20 1
|
|
21 22 2
|
|
22 22 3
|
|
23 23 4
|
|
24 26 5
|
|
25 26 1
|
|
26 26 1
|
|
27 28 2
|
|
28 28 3
|
|
29 29 4
|
|
30 30 1
|
|
-- two functions over the same window
|
|
-- an explain test would also be helpful, but it's too immature now and I don't
|
|
-- want to change reference all the time
|
|
select number, max(number) over (partition by intDiv(number, 3) order by number desc), count(number) over (partition by intDiv(number, 3) order by number desc) as m from numbers(7) order by number settings max_block_size = 2;
|
|
0 2 3
|
|
1 2 2
|
|
2 2 1
|
|
3 4 2
|
|
4 4 1
|
|
5 5 1
|
|
6 6 1
|
|
-- check that we can work with constant columns
|
|
select median(x) over (partition by x) from (select 1 x);
|
|
1
|
|
-- an empty window definition is valid as well
|
|
select groupArray(number) over () from numbers(3);
|
|
[0]
|
|
[0,1]
|
|
[0,1,2]
|
|
-- This one tests we properly process the window function arguments.
|
|
-- Seen errors like 'column `1` not found' from count(1).
|
|
select count(1) over (), max(number + 1) over () from numbers(3);
|
|
1 3
|
|
-- Should work in DISTINCT
|
|
select distinct sum(0) over () from numbers(2);
|
|
0
|
|
select distinct any(number) over () from numbers(2);
|
|
0
|
|
-- Various kinds of aliases are properly substituted into various parts of window
|
|
-- function definition.
|
|
with number + 1 as x select intDiv(number, 3) as y, sum(x + y) over (partition by y order by x) from numbers(7);
|
|
0 1
|
|
0 3
|
|
0 6
|
|
1 5
|
|
1 11
|
|
1 18
|
|
2 9
|
|
-- WINDOW clause
|
|
select 1 window w1 as ();
|
|
1
|
|
select sum(number) over w1, sum(number) over w2
|
|
from numbers(10)
|
|
window
|
|
w1 as (),
|
|
w2 as (partition by intDiv(number, 3))
|
|
;
|
|
0 0
|
|
1 1
|
|
3 3
|
|
6 3
|
|
10 7
|
|
15 12
|
|
21 6
|
|
28 13
|
|
36 21
|
|
45 9
|
|
select
|
|
sum(number) over w1,
|
|
sum(number) over (partition by intDiv(number, 3))
|
|
from numbers(10)
|
|
window
|
|
w1 as (partition by intDiv(number, 3))
|
|
;
|
|
0 0
|
|
1 1
|
|
3 3
|
|
3 3
|
|
7 7
|
|
12 12
|
|
6 6
|
|
13 13
|
|
21 21
|
|
9 9
|
|
-- ROWS frame
|
|
select
|
|
sum(number)
|
|
over (order by number rows between unbounded preceding and current row)
|
|
from numbers(3);
|
|
0
|
|
1
|
|
3
|
|
--select
|
|
-- sum(number)
|
|
-- over (order by number groups between unbounded preceding and current row)
|
|
--from numbers(3);
|
|
|
|
-- RANGE frame
|
|
-- Try some mutually prime sizes of partition, group and block, for the number
|
|
-- of rows that is their least common multiple so that we see all the interesting
|
|
-- corner cases.
|
|
select number, intDiv(number, 3) p, mod(number, 2) o, count(number) over w as c
|
|
from numbers(30)
|
|
window w as (partition by p order by o range unbounded preceding)
|
|
order by number
|
|
settings max_block_size = 5
|
|
;
|
|
0 0 0 2
|
|
1 0 1 3
|
|
2 0 0 2
|
|
3 1 1 3
|
|
4 1 0 1
|
|
5 1 1 3
|
|
6 2 0 2
|
|
7 2 1 3
|
|
8 2 0 2
|
|
9 3 1 3
|
|
10 3 0 1
|
|
11 3 1 3
|
|
12 4 0 2
|
|
13 4 1 3
|
|
14 4 0 2
|
|
15 5 1 3
|
|
16 5 0 1
|
|
17 5 1 3
|
|
18 6 0 2
|
|
19 6 1 3
|
|
20 6 0 2
|
|
21 7 1 3
|
|
22 7 0 1
|
|
23 7 1 3
|
|
24 8 0 2
|
|
25 8 1 3
|
|
26 8 0 2
|
|
27 9 1 3
|
|
28 9 0 1
|
|
29 9 1 3
|
|
select number, intDiv(number, 5) p, mod(number, 3) o, count(number) over w as c
|
|
from numbers(30)
|
|
window w as (partition by p order by o range unbounded preceding)
|
|
order by number
|
|
settings max_block_size = 2
|
|
;
|
|
0 0 0 2
|
|
1 0 1 4
|
|
2 0 2 5
|
|
3 0 0 2
|
|
4 0 1 4
|
|
5 1 2 5
|
|
6 1 0 2
|
|
7 1 1 3
|
|
8 1 2 5
|
|
9 1 0 2
|
|
10 2 1 3
|
|
11 2 2 5
|
|
12 2 0 1
|
|
13 2 1 3
|
|
14 2 2 5
|
|
15 3 0 2
|
|
16 3 1 4
|
|
17 3 2 5
|
|
18 3 0 2
|
|
19 3 1 4
|
|
20 4 2 5
|
|
21 4 0 2
|
|
22 4 1 3
|
|
23 4 2 5
|
|
24 4 0 2
|
|
25 5 1 3
|
|
26 5 2 5
|
|
27 5 0 1
|
|
28 5 1 3
|
|
29 5 2 5
|
|
select number, intDiv(number, 5) p, mod(number, 2) o, count(number) over w as c
|
|
from numbers(30)
|
|
window w as (partition by p order by o range unbounded preceding)
|
|
order by number
|
|
settings max_block_size = 3
|
|
;
|
|
0 0 0 3
|
|
1 0 1 5
|
|
2 0 0 3
|
|
3 0 1 5
|
|
4 0 0 3
|
|
5 1 1 5
|
|
6 1 0 2
|
|
7 1 1 5
|
|
8 1 0 2
|
|
9 1 1 5
|
|
10 2 0 3
|
|
11 2 1 5
|
|
12 2 0 3
|
|
13 2 1 5
|
|
14 2 0 3
|
|
15 3 1 5
|
|
16 3 0 2
|
|
17 3 1 5
|
|
18 3 0 2
|
|
19 3 1 5
|
|
20 4 0 3
|
|
21 4 1 5
|
|
22 4 0 3
|
|
23 4 1 5
|
|
24 4 0 3
|
|
25 5 1 5
|
|
26 5 0 2
|
|
27 5 1 5
|
|
28 5 0 2
|
|
29 5 1 5
|
|
select number, intDiv(number, 3) p, mod(number, 5) o, count(number) over w as c
|
|
from numbers(30)
|
|
window w as (partition by p order by o range unbounded preceding)
|
|
order by number
|
|
settings max_block_size = 2
|
|
;
|
|
0 0 0 1
|
|
1 0 1 2
|
|
2 0 2 3
|
|
3 1 3 2
|
|
4 1 4 3
|
|
5 1 0 1
|
|
6 2 1 1
|
|
7 2 2 2
|
|
8 2 3 3
|
|
9 3 4 3
|
|
10 3 0 1
|
|
11 3 1 2
|
|
12 4 2 1
|
|
13 4 3 2
|
|
14 4 4 3
|
|
15 5 0 1
|
|
16 5 1 2
|
|
17 5 2 3
|
|
18 6 3 2
|
|
19 6 4 3
|
|
20 6 0 1
|
|
21 7 1 1
|
|
22 7 2 2
|
|
23 7 3 3
|
|
24 8 4 3
|
|
25 8 0 1
|
|
26 8 1 2
|
|
27 9 2 1
|
|
28 9 3 2
|
|
29 9 4 3
|
|
select number, intDiv(number, 2) p, mod(number, 5) o, count(number) over w as c
|
|
from numbers(30)
|
|
window w as (partition by p order by o range unbounded preceding)
|
|
order by number
|
|
settings max_block_size = 3
|
|
;
|
|
0 0 0 1
|
|
1 0 1 2
|
|
2 1 2 1
|
|
3 1 3 2
|
|
4 2 4 2
|
|
5 2 0 1
|
|
6 3 1 1
|
|
7 3 2 2
|
|
8 4 3 1
|
|
9 4 4 2
|
|
10 5 0 1
|
|
11 5 1 2
|
|
12 6 2 1
|
|
13 6 3 2
|
|
14 7 4 2
|
|
15 7 0 1
|
|
16 8 1 1
|
|
17 8 2 2
|
|
18 9 3 1
|
|
19 9 4 2
|
|
20 10 0 1
|
|
21 10 1 2
|
|
22 11 2 1
|
|
23 11 3 2
|
|
24 12 4 2
|
|
25 12 0 1
|
|
26 13 1 1
|
|
27 13 2 2
|
|
28 14 3 1
|
|
29 14 4 2
|
|
select number, intDiv(number, 2) p, mod(number, 3) o, count(number) over w as c
|
|
from numbers(30)
|
|
window w as (partition by p order by o range unbounded preceding)
|
|
order by number
|
|
settings max_block_size = 5
|
|
;
|
|
0 0 0 1
|
|
1 0 1 2
|
|
2 1 2 2
|
|
3 1 0 1
|
|
4 2 1 1
|
|
5 2 2 2
|
|
6 3 0 1
|
|
7 3 1 2
|
|
8 4 2 2
|
|
9 4 0 1
|
|
10 5 1 1
|
|
11 5 2 2
|
|
12 6 0 1
|
|
13 6 1 2
|
|
14 7 2 2
|
|
15 7 0 1
|
|
16 8 1 1
|
|
17 8 2 2
|
|
18 9 0 1
|
|
19 9 1 2
|
|
20 10 2 2
|
|
21 10 0 1
|
|
22 11 1 1
|
|
23 11 2 2
|
|
24 12 0 1
|
|
25 12 1 2
|
|
26 13 2 2
|
|
27 13 0 1
|
|
28 14 1 1
|
|
29 14 2 2
|