\set ECHO none select count_distinct(x) over (order by x rows between unbounded preceding and current row) from test_data_1_20; count_distinct ---------------- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 (20 rows) select count_distinct(x) over (order by x rows between 10 preceding and 10 following) from test_data_1_25; count_distinct ---------------- 11 12 13 14 15 16 17 18 19 20 21 21 21 21 21 20 19 18 17 16 15 14 13 12 11 (25 rows) ROLLBACK;