SELECT depname, empno, salary, sum(salary) OVER (PARTITION BY depname) FROM empsalary ORDERBY depname, salary;
SELECT depname, empno, salary, rank() OVER (PARTITION BY depname ORDERBY salary) FROM empsalary;
-- with GROUP BY SELECT four, ten, SUM(SUM(four)) OVER (PARTITION BY four), AVG(ten) FROM tenk1 GROUPBY four, ten ORDERBY four, ten;
SELECT depname, empno, salary, sum(salary) OVER w FROM empsalary WINDOW w AS (PARTITION BY depname);
SELECT depname, empno, salary, rank() OVER w FROM empsalary WINDOW w AS (PARTITION BY depname ORDERBY salary) ORDERBY rank() OVER w;
-- empty window specification SELECT COUNT(*) OVER () FROM tenk1 WHERE unique2 < 10;
SELECT COUNT(*) OVER w FROM tenk1 WHERE unique2 < 10 WINDOW w AS ();
-- no window operation SELECT four FROM tenk1 WHEREFALSE WINDOW w AS (PARTITION BY ten);
-- cumulative aggregate SELECT sum(four) OVER (PARTITION BY ten ORDERBY unique2) AS sum_1, ten, four FROM tenk1 WHERE unique2 < 10;
SELECT row_number() OVER (ORDERBY unique2) FROM tenk1 WHERE unique2 < 10;
SELECT rank() OVER (PARTITION BY four ORDERBY ten) AS rank_1, ten, four FROM tenk1 WHERE unique2 < 10;
SELECT dense_rank() OVER (PARTITION BY four ORDERBY ten), ten, four FROM tenk1 WHERE unique2 < 10;
SELECT percent_rank() OVER (PARTITION BY four ORDERBY ten), ten, four FROM tenk1 WHERE unique2 < 10;
SELECT cume_dist() OVER (PARTITION BY four ORDERBY ten), ten, four FROM tenk1 WHERE unique2 < 10;
SELECT ntile(3) OVER (ORDERBY ten, four), ten, four FROM tenk1 WHERE unique2 < 10;
SELECT ntile(NULL) OVER (ORDERBY ten, four), ten, four FROM tenk1 LIMIT2;
SELECT lag(ten) OVER (PARTITION BY four ORDERBY ten), ten, four FROM tenk1 WHERE unique2 < 10;
SELECT lag(ten, four) OVER (PARTITION BY four ORDERBY ten), ten, four FROM tenk1 WHERE unique2 < 10;
SELECT lag(ten, four, 0) OVER (PARTITION BY four ORDERBY ten), ten, four FROM tenk1 WHERE unique2 < 10; SELECT lag(ten, four, 0.7) OVER (PARTITION BY four ORDERBY ten), ten, four FROM tenk1 WHERE unique2 < 10ORDERBY four, ten;
SELECT lead(ten) OVER (PARTITION BY four ORDERBY ten), ten, four FROM tenk1 WHERE unique2 < 10;
SELECT lead(ten * 2, 1) OVER (PARTITION BY four ORDERBY ten), ten, four FROM tenk1 WHERE unique2 < 10;
SELECT lead(ten * 2, 1, -1) OVER (PARTITION BY four ORDERBY ten), ten, four FROM tenk1 WHERE unique2 < 10; SELECT lead(ten * 2, 1, -1.4) OVER (PARTITION BY four ORDERBY ten), ten, four FROM tenk1 WHERE unique2 < 10ORDERBY four, ten;
SELECT first_value(ten) OVER (PARTITION BY four ORDERBY ten), ten, four FROM tenk1 WHERE unique2 < 10;
-- last_value returns the last row of the frame, which is CURRENT ROW in ORDER BY window. SELECT last_value(four) OVER (ORDERBY ten), ten, four FROM tenk1 WHERE unique2 < 10;
SELECT last_value(ten) OVER (PARTITION BY four), ten, four FROM
(SELECT * FROM tenk1 WHERE unique2 < 10ORDERBY four, ten)s ORDERBY four, ten;
SELECT nth_value(ten, four + 1) OVER (PARTITION BY four), ten, four FROM (SELECT * FROM tenk1 WHERE unique2 < 10ORDERBY four, ten)s;
SELECT ten, two, sum(hundred) AS gsum, sum(sum(hundred)) OVER (PARTITION BY two ORDERBY ten) AS wsum FROM tenk1 GROUPBY ten, two;
SELECT count(*) OVER (PARTITION BY four), four FROM (SELECT * FROM tenk1 WHERE two = 1)s WHERE unique2 < 10;
SELECT (count(*) OVER (PARTITION BY four ORDERBY ten) +
sum(hundred) OVER (PARTITION BY four ORDERBY ten))::varcharAS cntsum FROM tenk1 WHERE unique2 < 10;
-- opexpr with different windows evaluation. SELECT * FROM( SELECT count(*) OVER (PARTITION BY four ORDERBY ten) +
sum(hundred) OVER (PARTITION BY two ORDERBY ten) AS total,
count(*) OVER (PARTITION BY four ORDERBY ten) AS fourcount,
sum(hundred) OVER (PARTITION BY two ORDERBY ten) AS twosum FROM tenk1
)sub WHERE total <> fourcount + twosum;
SELECT avg(four) OVER (PARTITION BY four ORDERBY thousand / 100) FROM tenk1 WHERE unique2 < 10;
SELECT ten, two, sum(hundred) AS gsum, sum(sum(hundred)) OVER win AS wsum FROM tenk1 GROUPBY ten, two WINDOW win AS (PARTITION BY two ORDERBY ten);
-- more than one window with GROUP BY SELECT sum(salary),
row_number() OVER (ORDERBY depname),
sum(sum(salary)) OVER (ORDERBY depname DESC) FROM empsalary GROUPBY depname;
-- identical windows with different names SELECT sum(salary) OVER w1, count(*) OVER w2 FROM empsalary WINDOW w1 AS (ORDERBY salary), w2 AS (ORDERBY salary);
-- subplan SELECT lead(ten, (SELECT two FROM tenk1 WHERE s.unique2 = unique2)) OVER (PARTITION BY four ORDERBY ten) FROM tenk1 s WHERE unique2 < 10;
-- empty table SELECT count(*) OVER (PARTITION BY four) FROM (SELECT * FROM tenk1 WHEREFALSE)s;
-- mixture of agg/wfunc in the same window SELECT sum(salary) OVER w, rank() OVER w FROM empsalary WINDOW w AS (PARTITION BY depname ORDERBY salary DESC);
-- strict aggs SELECT empno, depname, salary, bonus, depadj, MIN(bonus) OVER (ORDERBY empno), MAX(depadj) OVER () FROM( SELECT *, CASEWHEN enroll_date < '2008-01-01'THEN2008 - extract(YEAR FROM enroll_date) END * 500AS bonus, CASEWHEN
AVG(salary) OVER (PARTITION BY depname) < salary THEN200 END AS depadj FROM empsalary
)s;
-- window function over ungrouped agg over empty row set (bug before 9.1) SELECT SUM(COUNT(f1)) OVER () FROM int4_tbl WHERE f1=42;
-- window function with ORDER BY an expression involving aggregates (9.1 bug) select ten,
sum(unique1) + sum(unique2) as res,
rank() over (orderby sum(unique1) + sum(unique2)) as rank from tenk1 groupby ten orderby ten;
-- window and aggregate with GROUP BY expression (9.2 bug) explain (costs off) select first_value(max(x)) over (), y from (select unique1 as x, ten+four as y from tenk1) ss groupby y;
-- window functions returning pass-by-ref values from different rows select x, lag(x, 1) over (orderby x), lead(x, 3) over (orderby x) from (select x::numericas x from generate_series(1,10) x);
-- test non-default frame specifications SELECT four, ten,
sum(ten) over (partition by four orderby ten),
last_value(ten) over (partition by four orderby ten) FROM (selectdistinct ten, four from tenk1) ss;
SELECT four, ten,
sum(ten) over (partition by four orderby ten range between unbounded preceding and current row),
last_value(ten) over (partition by four orderby ten range between unbounded preceding and current row) FROM (selectdistinct ten, four from tenk1) ss;
SELECT four, ten,
sum(ten) over (partition by four orderby ten range between unbounded preceding and unbounded following),
last_value(ten) over (partition by four orderby ten range between unbounded preceding and unbounded following) FROM (selectdistinct ten, four from tenk1) ss;
SELECT four, ten/4as two,
sum(ten/4) over (partition by four orderby ten/4 range between unbounded preceding and current row),
last_value(ten/4) over (partition by four orderby ten/4 range between unbounded preceding and current row) FROM (selectdistinct ten, four from tenk1) ss;
SELECT four, ten/4as two,
sum(ten/4) over (partition by four orderby ten/4 rows between unbounded preceding and current row),
last_value(ten/4) over (partition by four orderby ten/4 rows between unbounded preceding and current row) FROM (selectdistinct ten, four from tenk1) ss;
SELECT sum(unique1) over (orderby four range between current row and unbounded following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (rows between current row and unbounded following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (rows between2 preceding and2 following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (rows between2 preceding and2 following exclude no others),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (rows between2 preceding and2 following exclude current row),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (rows between2 preceding and2 following exclude group),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (rows between2 preceding and2 following exclude ties),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT first_value(unique1) over (ORDERBY four rows between current row and2 following exclude current row),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT first_value(unique1) over (ORDERBY four rows between current row and2 following exclude group),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT first_value(unique1) over (ORDERBY four rows between current row and2 following exclude ties),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT last_value(unique1) over (ORDERBY four rows between current row and2 following exclude current row),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT last_value(unique1) over (ORDERBY four rows between current row and2 following exclude group),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT last_value(unique1) over (ORDERBY four rows between current row and2 following exclude ties),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (rows between2 preceding and1 preceding),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (rows between1 following and3 following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (rows between unbounded preceding and1 following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (w range between current row and unbounded following),
unique1, four FROM tenk1 WHERE unique1 < 10 WINDOW w AS (orderby four);
SELECT sum(unique1) over (w range between unbounded preceding and current row exclude current row),
unique1, four FROM tenk1 WHERE unique1 < 10 WINDOW w AS (orderby four);
SELECT sum(unique1) over (w range between unbounded preceding and current row exclude group),
unique1, four FROM tenk1 WHERE unique1 < 10 WINDOW w AS (orderby four);
SELECT sum(unique1) over (w range between unbounded preceding and current row exclude ties),
unique1, four FROM tenk1 WHERE unique1 < 10 WINDOW w AS (orderby four);
SELECT first_value(unique1) over w,
nth_value(unique1, 2) over w AS nth_2,
last_value(unique1) over w, unique1, four FROM tenk1 WHERE unique1 < 10
WINDOW w AS (orderby four range between current row and unbounded following);
SELECT sum(unique1) over
(orderby unique1
rows (SELECT unique1 FROM tenk1 ORDERBY unique1 LIMIT1) + 1 PRECEDING),
unique1 FROM tenk1 WHERE unique1 < 10;
CREATE TEMP VIEW v_window AS SELECT i, sum(i) over (orderby i rows between1 preceding and1 following) as sum_rows FROM generate_series(1, 10) i;
SELECT * FROM v_window;
SELECT pg_get_viewdef('v_window');
CREATEORREPLACE TEMP VIEW v_window AS SELECT i, sum(i) over (orderby i rows between1 preceding and1 following
exclude current row) as sum_rows FROM generate_series(1, 10) i;
SELECT * FROM v_window;
SELECT pg_get_viewdef('v_window');
CREATEORREPLACE TEMP VIEW v_window AS SELECT i, sum(i) over (orderby i rows between1 preceding and1 following
exclude group) as sum_rows FROM generate_series(1, 10) i;
SELECT * FROM v_window;
SELECT pg_get_viewdef('v_window');
CREATEORREPLACE TEMP VIEW v_window AS SELECT i, sum(i) over (orderby i rows between1 preceding and1 following
exclude ties) as sum_rows FROM generate_series(1, 10) i;
SELECT * FROM v_window;
SELECT pg_get_viewdef('v_window');
CREATEORREPLACE TEMP VIEW v_window AS SELECT i, sum(i) over (orderby i rows between1 preceding and1 following
exclude no others) as sum_rows FROM generate_series(1, 10) i;
SELECT * FROM v_window;
SELECT pg_get_viewdef('v_window');
CREATEORREPLACE TEMP VIEW v_window AS SELECT i, sum(i) over (orderby i groups between1 preceding and1 following) as sum_rows FROM generate_series(1, 10) i;
SELECT * FROM v_window;
SELECT pg_get_viewdef('v_window');
DROP VIEW v_window;
CREATE TEMP VIEW v_window AS SELECT i, min(i) over (orderby i range between'1 day' preceding and'10 days' following) as min_i FROM generate_series(now(), now()+'100 days'::interval, '1 hour') i;
SELECT pg_get_viewdef('v_window');
-- test overflow frame specifications SELECT sum(unique1) over (rows between current row and9223372036854775807 following exclude current row),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (rows between9223372036854775807 following and1 following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT last_value(unique1) over (ORDERBY four rows between current row and9223372036854775807 following exclude current row),
unique1, four FROM tenk1 WHERE unique1 < 10;
-- These test GROUPS mode with an offset large enough to cause overflow when -- added to currentgroup. Although the overflow doesn't produce visibly wrong -- results (due to the incremental nature of group pointer advancement), we -- still need to protect against it as signed integer overflow is undefined -- behavior in C. SELECT sum(unique1) over (ORDERBY four groups between current row and9223372036854775807 following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (ORDERBY four groups between9223372036854775807 following and unbounded following),
unique1, four FROM tenk1 WHERE unique1 < 10;
-- RANGE offset PRECEDING/FOLLOWING tests
SELECT sum(unique1) over (orderby four range between2::int8 preceding and1::int2 preceding),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four desc range between2::int8 preceding and1::int2 preceding),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four range between2::int8 preceding and1::int2 preceding exclude no others),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four range between2::int8 preceding and1::int2 preceding exclude current row),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four range between2::int8 preceding and1::int2 preceding exclude group),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four range between2::int8 preceding and1::int2 preceding exclude ties),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four range between2::int8 preceding and6::int2 following exclude ties),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four range between2::int8 preceding and6::int2 following exclude group),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (partition by four orderby unique1 range between5::int8 preceding and6::int2 following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (partition by four orderby unique1 range between5::int8 preceding and6::int2 following
exclude current row),unique1, four FROM tenk1 WHERE unique1 < 10;
select sum(salary) over (orderby enroll_date range between'1 year'::interval preceding and'1 year'::interval following),
salary, enroll_date from empsalary;
select sum(salary) over (orderby enroll_date desc range between'1 year'::interval preceding and'1 year'::interval following),
salary, enroll_date from empsalary;
select sum(salary) over (orderby enroll_date desc range between'1 year'::interval following and'1 year'::interval following),
salary, enroll_date from empsalary;
select sum(salary) over (orderby enroll_date range between'1 year'::interval preceding and'1 year'::interval following
exclude current row), salary, enroll_date from empsalary;
select sum(salary) over (orderby enroll_date range between'1 year'::interval preceding and'1 year'::interval following
exclude group), salary, enroll_date from empsalary;
select sum(salary) over (orderby enroll_date range between'1 year'::interval preceding and'1 year'::interval following
exclude ties), salary, enroll_date from empsalary;
select first_value(salary) over(orderby salary range between1000 preceding and1000 following),
lead(salary) over(orderby salary range between1000 preceding and1000 following),
nth_value(salary, 1) over(orderby salary range between1000 preceding and1000 following),
salary from empsalary;
select last_value(salary) over(orderby salary range between1000 preceding and1000 following),
lag(salary) over(orderby salary range between1000 preceding and1000 following),
salary from empsalary;
select first_value(salary) over(orderby salary range between1000 following and3000 following
exclude current row),
lead(salary) over(orderby salary range between1000 following and3000 following exclude ties),
nth_value(salary, 1) over(orderby salary range between1000 following and3000 following
exclude ties),
salary from empsalary;
select last_value(salary) over(orderby salary range between1000 following and3000 following
exclude group),
lag(salary) over(orderby salary range between1000 following and3000 following exclude group),
salary from empsalary;
select first_value(salary) over(orderby enroll_date range between unbounded preceding and'1 year'::interval following
exclude ties),
last_value(salary) over(orderby enroll_date range between unbounded preceding and'1 year'::interval following),
salary, enroll_date from empsalary;
select first_value(salary) over(orderby enroll_date range between unbounded preceding and'1 year'::interval following
exclude ties),
last_value(salary) over(orderby enroll_date range between unbounded preceding and'1 year'::interval following
exclude ties),
salary, enroll_date from empsalary;
select first_value(salary) over(orderby enroll_date range between unbounded preceding and'1 year'::interval following
exclude group),
last_value(salary) over(orderby enroll_date range between unbounded preceding and'1 year'::interval following
exclude group),
salary, enroll_date from empsalary;
select first_value(salary) over(orderby enroll_date range between unbounded preceding and'1 year'::interval following
exclude current row),
last_value(salary) over(orderby enroll_date range between unbounded preceding and'1 year'::interval following
exclude current row),
salary, enroll_date from empsalary;
-- RANGE offset PRECEDING/FOLLOWING with null values select x, y,
first_value(y) over w,
last_value(y) over w from
(select x, x as y from generate_series(1,5) as x unionallselectnull, 42 unionallselectnull, 43) ss
window w as
(orderby x asc nulls first range between2 preceding and2 following);
select x, y,
first_value(y) over w,
last_value(y) over w from
(select x, x as y from generate_series(1,5) as x unionallselectnull, 42 unionallselectnull, 43) ss
window w as
(orderby x asc nulls last range between2 preceding and2 following);
select x, y,
first_value(y) over w,
last_value(y) over w from
(select x, x as y from generate_series(1,5) as x unionallselectnull, 42 unionallselectnull, 43) ss
window w as
(orderby x desc nulls first range between2 preceding and2 following);
select x, y,
first_value(y) over w,
last_value(y) over w from
(select x, x as y from generate_series(1,5) as x unionallselectnull, 42 unionallselectnull, 43) ss
window w as
(orderby x desc nulls last range between2 preceding and2 following);
-- There is a syntactic ambiguity in the SQL standard. Since -- UNBOUNDED is a non-reserved word, it could be the name of a -- function parameter and be used as an expression. There is a -- grammar hack to resolve such cases as the keyword. The following -- tests record this behavior.
CREATE FUNCTION unbounded_syntax_test1a(x int) RETURNS TABLE (a int, b int, c int)
LANGUAGE SQL
BEGIN ATOMIC SELECT sum(unique1) over (rows between x preceding and x following),
unique1, four FROM tenk1 WHERE unique1 < 10;
END;
CREATE FUNCTION unbounded_syntax_test1b(x int) RETURNS TABLE (a int, b int, c int)
LANGUAGE SQL AS $$ SELECT sum(unique1) over (rows between x preceding and x following),
unique1, four FROM tenk1 WHERE unique1 < 10;
$$;
-- These will apply the argument to the window specification inside the function. SELECT * FROM unbounded_syntax_test1a(2); SELECT * FROM unbounded_syntax_test1b(2);
CREATE FUNCTION unbounded_syntax_test2a(unbounded int) RETURNS TABLE (a int, b int, c int)
LANGUAGE SQL
BEGIN ATOMIC SELECT sum(unique1) over (rows between unbounded preceding and unbounded following),
unique1, four FROM tenk1 WHERE unique1 < 10;
END;
CREATE FUNCTION unbounded_syntax_test2b(unbounded int) RETURNS TABLE (a int, b int, c int)
LANGUAGE SQL AS $$ SELECT sum(unique1) over (rows between unbounded preceding and unbounded following),
unique1, four FROM tenk1 WHERE unique1 < 10;
$$;
-- These will not apply the argument but instead treat UNBOUNDED as a keyword. SELECT * FROM unbounded_syntax_test2a(2); SELECT * FROM unbounded_syntax_test2b(2);
DROP FUNCTION unbounded_syntax_test1a, unbounded_syntax_test1b,
unbounded_syntax_test2a, unbounded_syntax_test2b;
-- Other tests with token UNBOUNDED in potentially problematic position CREATE FUNCTION unbounded(x int) RETURNS int LANGUAGE SQL IMMUTABLE RETURN x;
SELECT sum(unique1) over (rows between1 preceding and1 following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (rows between unbounded(1) preceding and unbounded(1) following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (rows between unbounded.x preceding and unbounded.x following),
unique1, four FROM tenk1, (values (1)) as unbounded(x) WHERE unique1 < 10;
DROP FUNCTION unbounded;
-- Check overflow behavior for various integer sizes
select x, last_value(x) over (orderby x::smallint range between current row and2147450884 following) from generate_series(32764, 32766) x;
select x, last_value(x) over (orderby x::smallintdesc range between current row and2147450885 following) from generate_series(-32766, -32764) x;
select x, last_value(x) over (orderby x range between current row and4 following) from generate_series(2147483644, 2147483646) x;
select x, last_value(x) over (orderby x desc range between current row and5 following) from generate_series(-2147483646, -2147483644) x;
select x, last_value(x) over (orderby x range between current row and4 following) from generate_series(9223372036854775804, 9223372036854775806) x;
select x, last_value(x) over (orderby x desc range between current row and5 following) from generate_series(-9223372036854775806, -9223372036854775804) x;
select id, f_float4, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_float4 range between 1 preceding and1 following); select id, f_float4, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_float4 range between 1 preceding and1.1::float4 following); select id, f_float4, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_float4 range between 'inf' preceding and'inf' following); select id, f_float4, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_float4 range between 'inf' preceding and'inf' preceding); select id, f_float4, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_float4 range between 'inf' following and'inf' following); select id, f_float4, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_float4 range between 1.1 preceding and'NaN' following); -- error, NaN disallowed
select id, f_float8, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_float8 range between 1 preceding and1 following); select id, f_float8, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_float8 range between 1 preceding and1.1::float8 following); select id, f_float8, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_float8 range between 'inf' preceding and'inf' following); select id, f_float8, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_float8 range between 'inf' preceding and'inf' preceding); select id, f_float8, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_float8 range between 'inf' following and'inf' following); select id, f_float8, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_float8 range between 1.1 preceding and'NaN' following); -- error, NaN disallowed
select id, f_numeric, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_numeric range between 1 preceding and1 following); select id, f_numeric, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_numeric range between 1 preceding and1.1::numeric following); select id, f_numeric, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_numeric range between 1 preceding and1.1::float8 following); -- currently unsupported select id, f_numeric, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_numeric range between 'inf' preceding and'inf' following); select id, f_numeric, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_numeric range between 'inf' preceding and'inf' preceding); select id, f_numeric, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_numeric range between 'inf' following and'inf' following); select id, f_numeric, first_value(id) over w, last_value(id) over w from numerics
window w as (orderby f_numeric range between 1.1 preceding and'NaN' following); -- error, NaN disallowed
-- Test in_range for other datetime datatypes
create temp table datetimes(
id int,
f_time time,
f_timetz timetz,
f_interval interval,
f_timestamptz timestamptz,
f_timestamp timestamp
);
select id, f_time, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_time range between '70 min'::interval preceding and'2 hours'::interval following);
select id, f_time, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_time desc range between '70 min' preceding and'2 hours' following);
select id, f_time, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_time desc range between '-70 min' preceding and'2 hours' following); -- error, negative offset disallowed
select id, f_time, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_time range between 'infinity'::interval preceding and'infinity'::interval following);
select id, f_time, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_time range between 'infinity'::interval preceding and'infinity'::interval preceding);
select id, f_time, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_time range between 'infinity'::interval following and'infinity'::interval following);
select id, f_time, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_time range between '-infinity'::interval following and 'infinity'::interval following); -- error, negative offset disallowed
select id, f_timetz, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timetz range between '70 min'::interval preceding and'2 hours'::interval following);
select id, f_timetz, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timetz desc range between '70 min' preceding and'2 hours' following);
select id, f_timetz, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timetz desc range between '70 min' preceding and'-2 hours' following); -- error, negative offset disallowed
select id, f_timetz, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timetz range between 'infinity'::interval preceding and'infinity'::interval following);
select id, f_timetz, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timetz range between 'infinity'::interval preceding and'infinity'::interval preceding);
select id, f_timetz, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timetz range between 'infinity'::interval following and'infinity'::interval following);
select id, f_timetz, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timetz range between 'infinity'::interval following and '-infinity'::interval following); -- error, negative offset disallowed
select id, f_interval, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_interval range between '1 year'::interval preceding and'1 year'::interval following);
select id, f_interval, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_interval desc range between '1 year' preceding and'1 year' following);
select id, f_interval, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_interval desc range between '-1 year' preceding and'1 year' following); -- error, negative offset disallowed
select id, f_interval, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_interval range between 'infinity'::interval preceding and'infinity'::interval following);
select id, f_interval, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_interval range between 'infinity'::interval preceding and'infinity'::interval preceding);
select id, f_interval, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_interval range between 'infinity'::interval following and'infinity'::interval following);
select id, f_interval, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_interval range between '-infinity'::interval following and 'infinity'::interval following); -- error, negative offset disallowed
select id, f_timestamptz, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timestamptz range between '1 year'::interval preceding and'1 year'::interval following);
select id, f_timestamptz, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timestamptz desc range between '1 year' preceding and'1 year' following);
select id, f_timestamptz, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timestamptz desc range between '1 year' preceding and'-1 year' following); -- error, negative offset disallowed
select id, f_timestamptz, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timestamptz range between 'infinity'::interval preceding and'infinity'::interval following);
select id, f_timestamptz, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timestamptz range between 'infinity'::interval preceding and'infinity'::interval preceding);
select id, f_timestamptz, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timestamptz range between 'infinity'::interval following and'infinity'::interval following);
select id, f_timestamptz, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timestamptz range between '-infinity'::interval following and 'infinity'::interval following); -- error, negative offset disallowed
select id, f_timestamp, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timestamp range between '1 year'::interval preceding and'1 year'::interval following);
select id, f_timestamp, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timestamp desc range between '1 year' preceding and'1 year' following);
select id, f_timestamp, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timestamp desc range between '-1 year' preceding and'1 year' following); -- error, negative offset disallowed
select id, f_timestamp, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timestamp range between 'infinity'::interval preceding and'infinity'::interval following);
select id, f_timestamp, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timestamp range between 'infinity'::interval preceding and'infinity'::interval preceding);
select id, f_timestamp, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timestamp range between 'infinity'::interval following and'infinity'::interval following);
select id, f_timestamp, first_value(id) over w, last_value(id) over w from datetimes
window w as (orderby f_timestamp range between '-infinity'::interval following and 'infinity'::interval following); -- error, negative offset disallowed
-- RANGE offset PRECEDING/FOLLOWING error cases select sum(salary) over (orderby enroll_date, salary range between'1 year'::interval preceding and'2 years'::interval following
exclude ties), salary, enroll_date from empsalary;
select sum(salary) over (range between'1 year'::interval preceding and'2 years'::interval following
exclude ties), salary, enroll_date from empsalary;
select sum(salary) over (orderby depname range between'1 year'::interval preceding and'2 years'::interval following
exclude ties), salary, enroll_date from empsalary;
select max(enroll_date) over (orderby enroll_date range between1 preceding and2 following
exclude ties), salary, enroll_date from empsalary;
select max(enroll_date) over (orderby salary range between -1 preceding and2 following
exclude ties), salary, enroll_date from empsalary;
select max(enroll_date) over (orderby salary range between1 preceding and -2 following
exclude ties), salary, enroll_date from empsalary;
select max(enroll_date) over (orderby salary range between'1 year'::interval preceding and'2 years'::interval following
exclude ties), salary, enroll_date from empsalary;
select max(enroll_date) over (orderby enroll_date range between'1 year'::interval preceding and'-2 years'::interval following
exclude ties), salary, enroll_date from empsalary;
-- GROUPS tests
SELECT sum(unique1) over (orderby four groups between unbounded preceding and current row),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four groups between unbounded preceding and unbounded following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four groups between current row and unbounded following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four groups between1 preceding and unbounded following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four groups between1 following and unbounded following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four groups between unbounded preceding and2 following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four groups between2 preceding and1 preceding),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four groups between2 preceding and1 following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four groups between0 preceding and0 following),
unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four groups between2 preceding and1 following
exclude current row), unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four groups between2 preceding and1 following
exclude group), unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (orderby four groups between2 preceding and1 following
exclude ties), unique1, four FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (partition by ten orderby four groups between0 preceding and0 following),unique1, four, ten FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (partition by ten orderby four groups between0 preceding and0 following exclude current row), unique1, four, ten FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (partition by ten orderby four groups between0 preceding and0 following exclude group), unique1, four, ten FROM tenk1 WHERE unique1 < 10;
SELECT sum(unique1) over (partition by ten orderby four groups between0 preceding and0 following exclude ties), unique1, four, ten FROM tenk1 WHERE unique1 < 10;
select first_value(salary) over(orderby enroll_date groups between1 preceding and1 following),
lead(salary) over(orderby enroll_date groups between1 preceding and1 following),
nth_value(salary, 1) over(orderby enroll_date groups between1 preceding and1 following),
salary, enroll_date from empsalary;
select last_value(salary) over(orderby enroll_date groups between1 preceding and1 following),
lag(salary) over(orderby enroll_date groups between1 preceding and1 following),
salary, enroll_date from empsalary;
select first_value(salary) over(orderby enroll_date groups between1 following and3 following
exclude current row),
lead(salary) over(orderby enroll_date groups between1 following and3 following exclude ties),
nth_value(salary, 1) over(orderby enroll_date groups between1 following and3 following
exclude ties),
salary, enroll_date from empsalary;
select last_value(salary) over(orderby enroll_date groups between1 following and3 following
exclude group),
lag(salary) over(orderby enroll_date groups between1 following and3 following exclude group),
salary, enroll_date from empsalary;
-- Show differences in offset interpretation between ROWS, RANGE, and GROUPS WITH cte (x) AS ( SELECT * FROM generate_series(1, 35, 2)
) SELECT x, (sum(x) over w) FROM cte
WINDOW w AS (ORDERBY x rows between1 preceding and1 following);
WITH cte (x) AS ( SELECT * FROM generate_series(1, 35, 2)
) SELECT x, (sum(x) over w) FROM cte
WINDOW w AS (ORDERBY x range between1 preceding and1 following);
WITH cte (x) AS ( SELECT * FROM generate_series(1, 35, 2)
) SELECT x, (sum(x) over w) FROM cte
WINDOW w AS (ORDERBY x groups between1 preceding and1 following);
WITH cte (x) AS ( select1unionallselect1unionallselect1unionall SELECT * FROM generate_series(5, 49, 2)
) SELECT x, (sum(x) over w) FROM cte
WINDOW w AS (ORDERBY x rows between1 preceding and1 following);
WITH cte (x) AS ( select1unionallselect1unionallselect1unionall SELECT * FROM generate_series(5, 49, 2)
) SELECT x, (sum(x) over w) FROM cte
WINDOW w AS (ORDERBY x range between1 preceding and1 following);
WITH cte (x) AS ( select1unionallselect1unionallselect1unionall SELECT * FROM generate_series(5, 49, 2)
) SELECT x, (sum(x) over w) FROM cte
WINDOW w AS (ORDERBY x groups between1 preceding and1 following);
-- with UNION SELECT count(*) OVER (PARTITION BY four) FROM (SELECT * FROM tenk1 UNIONALLSELECT * FROM tenk2)s LIMIT0;
select f1, sum(f1) over (partition by f1
range between1 preceding and1 following) from t1 where f1 = f2; -- error, must have order by explain (costs off) select f1, sum(f1) over (partition by f1 orderby f2
range between1 preceding and1 following) from t1 where f1 = f2; select f1, sum(f1) over (partition by f1 orderby f2
range between1 preceding and1 following) from t1 where f1 = f2; select f1, sum(f1) over (partition by f1, f1 orderby f2
range between2 preceding and1 preceding) from t1 where f1 = f2; select f1, sum(f1) over (partition by f1, f2 orderby f2
range between1 following and2 following) from t1 where f1 = f2;
select f1, sum(f1) over (partition by f1
groups between1 preceding and1 following) from t1 where f1 = f2; -- error, must have order by explain (costs off) select f1, sum(f1) over (partition by f1 orderby f2
groups between1 preceding and1 following) from t1 where f1 = f2; select f1, sum(f1) over (partition by f1 orderby f2
groups between1 preceding and1 following) from t1 where f1 = f2; select f1, sum(f1) over (partition by f1, f1 orderby f2
groups between2 preceding and1 preceding) from t1 where f1 = f2; select f1, sum(f1) over (partition by f1, f2 orderby f2
groups between1 following and2 following) from t1 where f1 = f2;
-- ordering by a non-integer constant is allowed SELECT rank() OVER (ORDERBY length('abc'));
-- can't order by another window function SELECT rank() OVER (ORDERBY rank() OVER (ORDERBY random()));
-- some other errors SELECT * FROM empsalary WHERE row_number() OVER (ORDERBY salary) < 10;
SELECT * FROM empsalary INNERJOIN tenk1 ON row_number() OVER (ORDERBY salary) < 10;
SELECT rank() OVER (ORDERBY1), count(*) FROM empsalary GROUPBY1;
SELECT * FROM rank() OVER (ORDERBY random());
DELETEFROM empsalary WHERE (rank() OVER (ORDERBY random())) > 10;
DELETEFROM empsalary RETURNING rank() OVER (ORDERBY random());
SELECT count(*) OVER w FROM tenk1 WINDOW w AS (ORDERBY unique1), w AS (ORDERBY unique1);
SELECT rank() OVER (PARTITION BY four, ORDERBY ten) FROM tenk1;
SELECT count() OVER () FROM tenk1;
SELECT generate_series(1, 100) OVER () FROM empsalary;
SELECT ntile(0) OVER (ORDERBY ten), ten, four FROM tenk1;
SELECT nth_value(four, 0) OVER (ORDERBY ten), ten, four FROM tenk1;
-- filter
SELECT sum(salary), row_number() OVER (ORDERBY depname), sum(
sum(salary) FILTER (WHERE enroll_date > '2007-01-01')
) FILTER (WHERE depname <> 'sales') OVER (ORDERBY depname DESC) AS"filtered_sum",
depname FROM empsalary GROUPBY depname;
-- -- Test SupportRequestOptimizeWindowClause's ability to de-duplicate -- WindowClauses --
-- Ensure WindowClause frameOptions are changed so that only a single -- WindowAgg exists in the plan. EXPLAIN (COSTS OFF) SELECT
empno,
depname,
row_number() OVER (PARTITION BY depname ORDERBY enroll_date) rn,
rank() OVER (PARTITION BY depname ORDERBY enroll_date ROWS BETWEEN
UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) rnk,
dense_rank() OVER (PARTITION BY depname ORDERBY enroll_date RANGE BETWEEN
CURRENT ROW AND CURRENT ROW) drnk,
ntile(10) OVER (PARTITION BY depname ORDERBY enroll_date RANGE BETWEEN
CURRENT ROW AND UNBOUNDED FOLLOWING) nt,
percent_rank() OVER (PARTITION BY depname ORDERBY enroll_date ROWS BETWEEN
CURRENT ROW AND UNBOUNDED FOLLOWING) pr,
cume_dist() OVER (PARTITION BY depname ORDERBY enroll_date RANGE BETWEEN
CURRENT ROW AND UNBOUNDED FOLLOWING) cd FROM empsalary;
-- Ensure WindowFuncs which cannot support their WindowClause's frameOptions -- being changed are untouched EXPLAIN (COSTS OFF, VERBOSE) SELECT
empno,
depname,
row_number() OVER (PARTITION BY depname ORDERBY enroll_date) rn,
rank() OVER (PARTITION BY depname ORDERBY enroll_date ROWS BETWEEN
UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) rnk,
count(*) OVER (PARTITION BY depname ORDERBY enroll_date RANGE BETWEEN
CURRENT ROW AND CURRENT ROW) cnt FROM empsalary;
-- Ensure the above query gives us the expected results SELECT
empno,
depname,
row_number() OVER (PARTITION BY depname ORDERBY enroll_date) rn,
rank() OVER (PARTITION BY depname ORDERBY enroll_date ROWS BETWEEN
UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) rnk,
count(*) OVER (PARTITION BY depname ORDERBY enroll_date RANGE BETWEEN
CURRENT ROW AND CURRENT ROW) cnt FROM empsalary;
-- Test pushdown of quals into a subquery containing window functions
-- pushdown is safe because all PARTITION BY clauses include depname: EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT depname,
sum(salary) OVER (PARTITION BY depname) depsalary,
min(salary) OVER (PARTITION BY depname || 'A', depname) depminsalary FROM empsalary) emp WHERE depname = 'sales';
-- pushdown is unsafe because there's a PARTITION BY clause without depname: EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT depname,
sum(salary) OVER (PARTITION BY enroll_date) enroll_salary,
min(salary) OVER (PARTITION BY depname) depminsalary FROM empsalary) emp WHERE depname = 'sales';
-- Test window function run conditions are properly pushed down into the -- WindowAgg EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
row_number() OVER (ORDERBY empno) rn FROM empsalary) emp WHERE rn < 3;
-- The following 3 statements should result the same result. SELECT * FROM
(SELECT empno,
row_number() OVER (ORDERBY empno) rn FROM empsalary) emp WHERE rn < 3;
SELECT * FROM
(SELECT empno,
row_number() OVER (ORDERBY empno) rn FROM empsalary) emp WHERE3 > rn;
SELECT * FROM
(SELECT empno,
row_number() OVER (ORDERBY empno) rn FROM empsalary) emp WHERE2 >= rn;
-- Ensure r <= 3 is pushed down into the run condition of the window agg EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
salary,
rank() OVER (ORDERBY salary DESC) r FROM empsalary) emp WHERE r <= 3;
SELECT * FROM
(SELECT empno,
salary,
rank() OVER (ORDERBY salary DESC) r FROM empsalary) emp WHERE r <= 3;
-- Ensure dr = 1 is converted to dr <= 1 to get all rows leading up to dr = 1 EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
salary,
dense_rank() OVER (ORDERBY salary DESC) dr FROM empsalary) emp WHERE dr = 1;
SELECT * FROM
(SELECT empno,
salary,
dense_rank() OVER (ORDERBY salary DESC) dr FROM empsalary) emp WHERE dr = 1;
-- Check COUNT() and COUNT(*) EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
salary,
count(*) OVER (ORDERBY salary DESC) c FROM empsalary) emp WHERE c <= 3;
SELECT * FROM
(SELECT empno,
salary,
count(*) OVER (ORDERBY salary DESC) c FROM empsalary) emp WHERE c <= 3;
EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
salary,
count(empno) OVER (ORDERBY salary DESC) c FROM empsalary) emp WHERE c <= 3;
SELECT * FROM
(SELECT empno,
salary,
count(empno) OVER (ORDERBY salary DESC) c FROM empsalary) emp WHERE c <= 3;
EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
salary,
count(*) OVER (ORDERBY salary DESC ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) c FROM empsalary) emp WHERE c >= 3;
EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
salary,
count(*) OVER () c FROM empsalary) emp WHERE11 <= c;
EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
salary,
count(*) OVER (ORDERBY salary DESC) c,
dense_rank() OVER (ORDERBY salary DESC) dr FROM empsalary) emp WHERE dr = 1;
-- Ensure we get a run condition when there's a PARTITION BY clause EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
depname,
row_number() OVER (PARTITION BY depname ORDERBY empno) rn FROM empsalary) emp WHERE rn < 3;
-- and ensure we get the correct results from the above plan SELECT * FROM
(SELECT empno,
depname,
row_number() OVER (PARTITION BY depname ORDERBY empno) rn FROM empsalary) emp WHERE rn < 3;
-- ensure that "unused" subquery columns are not removed when the column only -- exists in the run condition EXPLAIN (COSTS OFF) SELECT empno, depname FROM
(SELECT empno,
depname,
row_number() OVER (PARTITION BY depname ORDERBY empno) rn FROM empsalary) emp WHERE rn < 3;
-- likewise with count(empno) instead of row_number() EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
depname,
salary,
count(empno) OVER (PARTITION BY depname ORDERBY salary DESC) c FROM empsalary) emp WHERE c <= 3;
-- and again, check the results are what we expect. SELECT * FROM
(SELECT empno,
depname,
salary,
count(empno) OVER (PARTITION BY depname ORDERBY salary DESC) c FROM empsalary) emp WHERE c <= 3;
-- Ensure we get the correct run condition when the window function is both -- monotonically increasing and decreasing. EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
depname,
salary,
count(empno) OVER () c FROM empsalary) emp WHERE c = 1;
-- Try another case with a WindowFunc with a byref return type SELECT * FROM
(SELECT row_number() OVER (PARTITION BY salary) AS rn,
lead(depname) OVER (PARTITION BY salary) || ' Department'AS n_dep FROM empsalary) emp WHERE rn < 1;
-- Some more complex cases with multiple window clauses EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT *,
count(salary) OVER (PARTITION BY depname || '') c1, -- w1
row_number() OVER (PARTITION BY depname) rn, -- w2
count(*) OVER (PARTITION BY depname) c2, -- w2
count(*) OVER (PARTITION BY'' || depname) c3, -- w3
ntile(2) OVER (PARTITION BY depname) nt -- w2 FROM empsalary
) e WHERE rn <= 1AND c1 <= 3AND nt < 2;
-- Ensure we correctly filter out all of the run conditions from each window SELECT * FROM
(SELECT *,
count(salary) OVER (PARTITION BY depname || '') c1, -- w1
row_number() OVER (PARTITION BY depname) rn, -- w2
count(*) OVER (PARTITION BY depname) c2, -- w2
count(*) OVER (PARTITION BY'' || depname) c3, -- w3
ntile(2) OVER (PARTITION BY depname) nt -- w2 FROM empsalary
) e WHERE rn <= 1AND c1 <= 3AND nt < 2;
-- Ensure we remove references to reduced outer joins as nulling rels in run -- conditions EXPLAIN (COSTS OFF) SELECT1FROM
(SELECT ntile(e2.salary) OVER (PARTITION BY e1.depname) AS c FROM empsalary e1 LEFTJOIN empsalary e2 ONTRUE WHERE e1.empno = e2.empno) s WHERE s.c = 1;
-- Ensure the run condition optimization is used in cases where the WindowFunc -- has a Var from another query level EXPLAIN (COSTS OFF) SELECT1FROM
(SELECT ntile(s1.x) OVER () AS c FROM (SELECT (SELECT1) AS x) AS s1) s WHERE s.c = 1;
-- Tests to ensure we don't push down the run condition when it's not valid to -- do so.
-- Ensure we don't push down when the frame options show that the window -- function is not monotonically increasing EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
salary,
count(*) OVER (ORDERBY salary DESC ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) c FROM empsalary) emp WHERE c <= 3;
-- Ensure we don't push down when the window function's monotonic properties -- don't match that of the clauses. EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
salary,
count(*) OVER (ORDERBY salary) c FROM empsalary) emp WHERE3 <= c;
-- Ensure we don't use a run condition when there's a volatile function in the -- WindowFunc EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
salary,
count(random()) OVER (ORDERBY empno DESC) c FROM empsalary) emp WHERE c = 1;
-- Ensure we don't use a run condition when the WindowFunc contains subplans EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT empno,
salary,
count((SELECT1)) OVER (ORDERBY empno DESC) c FROM empsalary) emp WHERE c = 1;
-- Test Sort node collapsing EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT depname,
sum(salary) OVER (PARTITION BY depname orderby empno) depsalary,
min(salary) OVER (PARTITION BY depname, empno orderby enroll_date) depminsalary FROM empsalary) emp WHERE depname = 'sales';
-- Ensure that the evaluation order of the WindowAggs results in the WindowAgg -- with the same sort order that's required by the ORDER BY is evaluated last. EXPLAIN (COSTS OFF) SELECT empno,
enroll_date,
depname,
sum(salary) OVER (PARTITION BY depname orderby empno) depsalary,
min(salary) OVER (PARTITION BY depname orderby enroll_date) depminsalary FROM empsalary ORDERBY depname, empno;
-- As above, but with an adjusted ORDER BY to ensure the above plan didn't -- perform only 2 sorts by accident. EXPLAIN (COSTS OFF) SELECT empno,
enroll_date,
depname,
sum(salary) OVER (PARTITION BY depname orderby empno) depsalary,
min(salary) OVER (PARTITION BY depname orderby enroll_date) depminsalary FROM empsalary ORDERBY depname, enroll_date;
SET enable_hashagg TO off;
-- Ensure we don't get a sort for both DISTINCT and ORDER BY. We expect the -- sort for the DISTINCT to provide presorted input for the ORDER BY. EXPLAIN (COSTS OFF) SELECTDISTINCT
empno,
enroll_date,
depname,
sum(salary) OVER (PARTITION BY depname orderby empno) depsalary,
min(salary) OVER (PARTITION BY depname orderby enroll_date) depminsalary FROM empsalary ORDERBY depname, enroll_date;
-- As above but adjust the ORDER BY clause to help ensure the plan with the -- minimum amount of sorting wasn't a fluke. EXPLAIN (COSTS OFF) SELECTDISTINCT
empno,
enroll_date,
depname,
sum(salary) OVER (PARTITION BY depname orderby empno) depsalary,
min(salary) OVER (PARTITION BY depname orderby enroll_date) depminsalary FROM empsalary ORDERBY depname, empno;
RESET enable_hashagg;
-- Test Sort node reordering EXPLAIN (COSTS OFF) SELECT
lead(1) OVER (PARTITION BY depname ORDERBY salary, enroll_date),
lag(1) OVER (PARTITION BY depname ORDERBY salary,enroll_date,empno) FROM empsalary;
-- Test incremental sorting EXPLAIN (COSTS OFF) SELECT * FROM
(SELECT depname,
empno,
salary,
enroll_date,
row_number() OVER (PARTITION BY depname ORDERBY enroll_date) AS first_emp,
row_number() OVER (PARTITION BY depname ORDERBY enroll_date DESC) AS last_emp FROM empsalary) emp WHERE first_emp = 1OR last_emp = 1;
SELECT * FROM
(SELECT depname,
empno,
salary,
enroll_date,
row_number() OVER (PARTITION BY depname ORDERBY enroll_date) AS first_emp,
row_number() OVER (PARTITION BY depname ORDERBY enroll_date DESC) AS last_emp FROM empsalary) emp WHERE first_emp = 1OR last_emp = 1;
-- cleanup DROPTABLE empsalary;
-- test user-defined window function with named args and default args CREATE FUNCTION nth_value_def(val anyelement, n integer = 1) RETURNS anyelement
LANGUAGE internal WINDOW IMMUTABLE STRICT AS'window_nth_value';
SELECT nth_value_def(n := 2, val := ten) OVER (PARTITION BY four), ten, four FROM (SELECT * FROM tenk1 WHERE unique2 < 10ORDERBY four, ten) s;
SELECT nth_value_def(ten) OVER (PARTITION BY four), ten, four FROM (SELECT * FROM tenk1 WHERE unique2 < 10ORDERBY four, ten) s;
-- -- Test the basic moving-aggregate machinery --
-- create aggregates that record the series of transform calls (these are -- intentionally not true inverses)
CREATE FUNCTION logging_sfunc_nonstrict(text, anyelement) RETURNS text AS
$$ SELECT COALESCE($1, '') || '*' || quote_nullable($2) $$
LANGUAGE SQL IMMUTABLE;
CREATE FUNCTION logging_msfunc_nonstrict(text, anyelement) RETURNS text AS
$$ SELECT COALESCE($1, '') || '+' || quote_nullable($2) $$
LANGUAGE SQL IMMUTABLE;
CREATE FUNCTION logging_minvfunc_nonstrict(text, anyelement) RETURNS text AS
$$ SELECT $1 || '-' || quote_nullable($2) $$
LANGUAGE SQL IMMUTABLE;
-- test strict and non-strict cases SELECT
p::text || ',' || i::text || ':' || COALESCE(v::text, 'NULL') AS row,
logging_agg_nonstrict(v) over wnd as nstrict,
logging_agg_nonstrict_initcond(v) over wnd as nstrict_init,
logging_agg_strict(v::text) over wnd as strict,
logging_agg_strict_initcond(v) over wnd as strict_init FROM (VALUES
(1, 1, NULL),
(1, 2, 'a'),
(1, 3, 'b'),
(1, 4, NULL),
(1, 5, NULL),
(1, 6, 'c'),
(2, 1, NULL),
(2, 2, 'x'),
(3, 1, 'z')
) AS t(p, i, v)
WINDOW wnd AS (PARTITION BY P ORDERBY i ROWS BETWEEN1 PRECEDING AND CURRENT ROW) ORDERBY p, i;
-- and again, but with filter SELECT
p::text || ',' || i::text || ':' || CASEWHEN f THEN COALESCE(v::text, 'NULL') ELSE'-' END as row,
logging_agg_nonstrict(v) filter(where f) over wnd as nstrict_filt,
logging_agg_nonstrict_initcond(v) filter(where f) over wnd as nstrict_init_filt,
logging_agg_strict(v::text) filter(where f) over wnd as strict_filt,
logging_agg_strict_initcond(v) filter(where f) over wnd as strict_init_filt FROM (VALUES
(1, 1, true, NULL),
(1, 2, false, 'a'),
(1, 3, true, 'b'),
(1, 4, false, NULL),
(1, 5, false, NULL),
(1, 6, false, 'c'),
(2, 1, false, NULL),
(2, 2, true, 'x'),
(3, 1, true, 'z')
) AS t(p, i, f, v)
WINDOW wnd AS (PARTITION BY p ORDERBY i ROWS BETWEEN1 PRECEDING AND CURRENT ROW) ORDERBY p, i;
-- test that volatile arguments disable moving-aggregate mode SELECT
i::text || ':' || COALESCE(v::text, 'NULL') as row,
logging_agg_strict(v::text)
over wnd as inverse,
logging_agg_strict(v::text || CASEWHEN random() < 0then'?'ELSE'' END)
over wnd as noinverse FROM (VALUES
(1, 'a'),
(2, 'b'),
(3, 'c')
) AS t(i, v)
WINDOW wnd AS (ORDERBY i ROWS BETWEEN1 PRECEDING AND CURRENT ROW) ORDERBY i;
SELECT
i::text || ':' || COALESCE(v::text, 'NULL') as row,
logging_agg_strict(v::text) filter(wheretrue)
over wnd as inverse,
logging_agg_strict(v::text) filter(where random() >= 0)
over wnd as noinverse FROM (VALUES
(1, 'a'),
(2, 'b'),
(3, 'c')
) AS t(i, v)
WINDOW wnd AS (ORDERBY i ROWS BETWEEN1 PRECEDING AND CURRENT ROW) ORDERBY i;
-- test that non-overlapping windows don't use inverse transitions SELECT
logging_agg_strict(v::text) OVER wnd FROM (VALUES
(1, 'a'),
(2, 'b'),
(3, 'c')
) AS t(i, v)
WINDOW wnd AS (ORDERBY i ROWS BETWEEN CURRENT ROW AND CURRENT ROW) ORDERBY i;
-- test that returning NULL from the inverse transition functions -- restarts the aggregation from scratch. The second aggregate is supposed -- to test cases where only some aggregates restart, the third one checks -- that one aggregate restarting doesn't cause others to restart.
CREATE FUNCTION sum_int_randrestart_minvfunc(int4, int4) RETURNS int4AS
$$ SELECTCASEWHEN random() < 0.2THENNULLELSE $1 - $2 END $$
LANGUAGE SQL STRICT;
WITH
vs AS ( SELECT i, (random() * 100)::int4AS v FROM generate_series(1, 100) AS i
),
sum_following AS ( SELECT i, SUM(v) OVER
(ORDERBY i DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS s FROM vs
) SELECTDISTINCT
sum_following.s = sum_int_randomrestart(v) OVER fwd AS eq1,
-sum_following.s = sum_int_randomrestart(-v) OVER fwd AS eq2, 100*3+(vs.i-1)*3 = length(logging_agg_nonstrict(''::text) OVER fwd) AS eq3 FROM vs JOIN sum_following ON sum_following.i = vs.i
WINDOW fwd AS ( ORDERBY vs.i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
);
-- -- Test various built-in aggregates that have moving-aggregate support --
-- test inverse transition functions handle NULLs properly SELECT i,AVG(v::bigint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
SELECT i,AVG(v::int) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
SELECT i,AVG(v::smallint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
SELECT i,AVG(v::numeric) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,1.5),(2,2.5),(3,NULL),(4,NULL)) t(i,v);
SELECT i,AVG(v::interval) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,'1 sec'),(2,'2 sec'),(3,NULL),(4,NULL)) t(i,v);
-- moving aggregates over infinite intervals SELECT x
,avg(x) OVER(ROWS BETWEEN CURRENT ROW AND1 FOLLOWING ) as curr_next_avg
,avg(x) OVER(ROWS BETWEEN1 PRECEDING AND CURRENT ROW ) as prev_curr_avg
,sum(x) OVER(ROWS BETWEEN CURRENT ROW AND1 FOLLOWING ) as curr_next_sum
,sum(x) OVER(ROWS BETWEEN1 PRECEDING AND CURRENT ROW ) as prev_curr_sum FROM (VALUES (NULL::interval),
('infinity'::interval),
('-2147483648 days -2147483648 months -9223372036854775807 usecs'), -- extreme interval value
('-infinity'::interval),
('2147483647 days 2147483647 months 9223372036854775806 usecs'), -- extreme interval value
('infinity'::interval),
('6 days'::interval),
('7 days'::interval),
(NULL::interval),
('-infinity'::interval)) v(x);
--should fail. SELECT x, avg(x) OVER(ROWS BETWEEN CURRENT ROW AND2 FOLLOWING) FROM (VALUES (NULL::interval),
('3 days'::interval),
('infinity'::timestamptz - now()),
('6 days'::interval),
('-infinity'::interval)) v(x);
--should fail. SELECT x, sum(x) OVER(ROWS BETWEEN CURRENT ROW AND2 FOLLOWING) FROM (VALUES (NULL::interval),
('3 days'::interval),
('infinity'::timestamptz - now()),
('6 days'::interval),
('-infinity'::interval)) v(x);
SELECT i,SUM(v::smallint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
SELECT i,SUM(v::int) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
SELECT i,SUM(v::bigint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
SELECT i,SUM(v::money) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,'1.10'),(2,'2.20'),(3,NULL),(4,NULL)) t(i,v);
SELECT i,SUM(v::interval) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,'1 sec'),(2,'2 sec'),(3,NULL),(4,NULL)) t(i,v);
SELECT i,SUM(v::numeric) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,1.1),(2,2.2),(3,NULL),(4,NULL)) t(i,v);
SELECT SUM(n::numeric) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,1.01),(2,2),(3,3)) v(i,n);
SELECT i,COUNT(v) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
SELECT i,COUNT(*) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
SELECT VAR_POP(n::bigint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT VAR_POP(n::int) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT VAR_POP(n::smallint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT VAR_POP(n::numeric) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT VAR_SAMP(n::bigint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT VAR_SAMP(n::int) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT VAR_SAMP(n::smallint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT VAR_SAMP(n::numeric) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT VARIANCE(n::bigint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT VARIANCE(n::int) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT VARIANCE(n::smallint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT VARIANCE(n::numeric) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT STDDEV_POP(n::bigint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
SELECT STDDEV_POP(n::int) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
SELECT STDDEV_POP(n::smallint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
SELECT STDDEV_POP(n::numeric) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
SELECT STDDEV_SAMP(n::bigint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
SELECT STDDEV_SAMP(n::int) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
SELECT STDDEV_SAMP(n::smallint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
SELECT STDDEV_SAMP(n::numeric) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(1,NULL),(2,600),(3,470),(4,170),(5,430),(6,300)) r(i,n);
SELECT STDDEV(n::bigint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(0,NULL),(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT STDDEV(n::int) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(0,NULL),(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT STDDEV(n::smallint) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(0,NULL),(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
SELECT STDDEV(n::numeric) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM (VALUES(0,NULL),(1,600),(2,470),(3,170),(4,430),(5,300)) r(i,n);
-- test that inverse transition functions work with various frame options SELECT i,SUM(v::int) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND CURRENT ROW) FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
SELECT i,SUM(v::int) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND1 FOLLOWING) FROM (VALUES(1,1),(2,2),(3,NULL),(4,NULL)) t(i,v);
SELECT i,SUM(v::int) OVER (ORDERBY i ROWS BETWEEN1 PRECEDING AND1 FOLLOWING) FROM (VALUES(1,1),(2,2),(3,3),(4,4)) t(i,v);
-- ensure aggregate over numeric properly recovers from NaN values SELECT a, b,
SUM(b) OVER(ORDERBY A ROWS BETWEEN1 PRECEDING AND CURRENT ROW) FROM (VALUES(1,1::numeric),(2,2),(3,'NaN'),(4,3),(5,4)) t(a,b);
-- It might be tempting for someone to add an inverse trans function for -- float and double precision. This should not be done as it can give incorrect -- results. This test should fail if anyone ever does this without thinking too -- hard about it. SELECT to_char(SUM(n::float8) OVER (ORDERBY i ROWS BETWEEN CURRENT ROW AND1 FOLLOWING),'999999999999999999999D9') FROM (VALUES(1,1e20),(2,1)) n(i,n);
SELECT i, b, bool_and(b) OVER w, bool_or(b) OVER w FROM (VALUES (1,true), (2,true), (3,false), (4,false), (5,true)) v(i,b)
WINDOW w AS (ORDERBY i ROWS BETWEEN CURRENT ROW AND1 FOLLOWING);
-- -- Test WindowAgg costing takes into account the number of rows that need to -- be fetched before the first row can be output. --
-- Ensure we get a cheap start up plan as the WindowAgg can output the first -- row after reading 1 row from the join. EXPLAIN (COSTS OFF) SELECT COUNT(*) OVER (ORDERBY t1.unique1) FROM tenk1 t1 INNERJOIN tenk1 t2 ON t1.unique1 = t2.tenthous LIMIT1;
-- Ensure we get a cheap total plan. Lack of ORDER BY in the WindowClause -- means that all rows must be read from the join, so a cheap startup plan -- isn't a good choice. EXPLAIN (COSTS OFF) SELECT COUNT(*) OVER () FROM tenk1 t1 INNERJOIN tenk1 t2 ON t1.unique1 = t2.tenthous WHERE t2.two = 1 LIMIT1;
-- Ensure we get a cheap total plan. This time use UNBOUNDED FOLLOWING, which -- needs to read all join rows to output the first WindowAgg row. EXPLAIN (COSTS OFF) SELECT COUNT(*) OVER (ORDERBY t1.unique1 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM tenk1 t1 INNERJOIN tenk1 t2 ON t1.unique1 = t2.tenthous LIMIT1;
-- Ensure we get a cheap total plan. This time use 10000 FOLLOWING so we need -- to read all join rows. EXPLAIN (COSTS OFF) SELECT COUNT(*) OVER (ORDERBY t1.unique1 ROWS BETWEEN UNBOUNDED PRECEDING AND10000 FOLLOWING) FROM tenk1 t1 INNERJOIN tenk1 t2 ON t1.unique1 = t2.tenthous LIMIT1;
-- Tests for problems with failure to walk or mutate expressions -- within window frame clauses.
-- test walker (fails with collation error if expressions are not walked) SELECT array_agg(i) OVER w FROM generate_series(1,5) i
WINDOW w AS (ORDERBY i ROWS BETWEEN (('foo' < 'foobar')::integer) PRECEDING AND CURRENT ROW);
-- test mutator (fails when inlined if expressions are not mutated) CREATE FUNCTION pg_temp.f(group_size BIGINT) RETURNS SETOF integer[] AS $$ SELECT array_agg(s) OVER w FROM generate_series(1,5) s
WINDOW w AS (ORDERBY s ROWS BETWEEN CURRENT ROW AND GROUP_SIZE FOLLOWING)
$$ LANGUAGE SQL STABLE;
EXPLAIN (costs off) SELECT * FROM pg_temp.f(2); SELECT * FROM pg_temp.f(2);
Messung V0.5 in Prozent
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.34Angebot
(Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können 2026-08-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.