2008-12-28 19:54:01 +01:00
|
|
|
--
|
|
|
|
-- WINDOW FUNCTIONS
|
|
|
|
--
|
|
|
|
|
|
|
|
CREATE TEMPORARY TABLE empsalary (
|
|
|
|
depname varchar,
|
|
|
|
empno bigint,
|
|
|
|
salary int,
|
|
|
|
enroll_date date
|
|
|
|
);
|
|
|
|
|
|
|
|
INSERT INTO empsalary VALUES
|
|
|
|
('develop', 10, 5200, '2007-08-01'),
|
|
|
|
('sales', 1, 5000, '2006-10-01'),
|
|
|
|
('personnel', 5, 3500, '2007-12-10'),
|
|
|
|
('sales', 4, 4800, '2007-08-08'),
|
|
|
|
('personnel', 2, 3900, '2006-12-23'),
|
|
|
|
('develop', 7, 4200, '2008-01-01'),
|
|
|
|
('develop', 9, 4500, '2008-01-01'),
|
|
|
|
('sales', 3, 4800, '2007-08-01'),
|
|
|
|
('develop', 8, 6000, '2006-10-01'),
|
|
|
|
('develop', 11, 5200, '2007-08-15');
|
|
|
|
|
|
|
|
SELECT depname, empno, salary, sum(salary) OVER (PARTITION BY depname) FROM empsalary ORDER BY depname, salary;
|
|
|
|
|
|
|
|
SELECT depname, empno, salary, rank() OVER (PARTITION BY depname ORDER BY salary) FROM empsalary;
|
|
|
|
|
|
|
|
-- with GROUP BY
|
|
|
|
SELECT four, ten, SUM(SUM(four)) OVER (PARTITION BY four), AVG(ten) FROM tenk1
|
|
|
|
GROUP BY four, ten ORDER BY 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 ORDER BY salary) ORDER BY 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 WHERE FALSE WINDOW w AS (PARTITION BY ten);
|
|
|
|
|
|
|
|
-- cumulative aggregate
|
|
|
|
SELECT sum(four) OVER (PARTITION BY ten ORDER BY unique2) AS sum_1, ten, four FROM tenk1 WHERE unique2 < 10;
|
|
|
|
|
|
|
|
SELECT row_number() OVER (ORDER BY unique2) FROM tenk1 WHERE unique2 < 10;
|
|
|
|
|
|
|
|
SELECT rank() OVER (PARTITION BY four ORDER BY ten) AS rank_1, ten, four FROM tenk1 WHERE unique2 < 10;
|
|
|
|
|
|
|
|
SELECT dense_rank() OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
|
|
|
|
|
|
|
|
SELECT percent_rank() OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
|
|
|
|
|
|
|
|
SELECT cume_dist() OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
|
|
|
|
|
2008-12-29 03:58:11 +01:00
|
|
|
SELECT ntile(3) OVER (ORDER BY ten, four), ten, four FROM tenk1 WHERE unique2 < 10;
|
2008-12-28 19:54:01 +01:00
|
|
|
|
2008-12-29 03:58:11 +01:00
|
|
|
SELECT ntile(NULL) OVER (ORDER BY ten, four), ten, four FROM tenk1 LIMIT 2;
|
2008-12-28 19:54:01 +01:00
|
|
|
|
|
|
|
SELECT lag(ten) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
|
|
|
|
|
|
|
|
SELECT lag(ten, four) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
|
|
|
|
|
|
|
|
SELECT lag(ten, four, 0) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
|
|
|
|
|
|
|
|
SELECT lead(ten) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
|
|
|
|
|
|
|
|
SELECT lead(ten * 2, 1) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
|
|
|
|
|
|
|
|
SELECT lead(ten * 2, 1, -1) OVER (PARTITION BY four ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
|
|
|
|
|
|
|
|
SELECT first_value(ten) OVER (PARTITION BY four ORDER BY 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.
|
2010-11-23 21:27:50 +01:00
|
|
|
SELECT last_value(four) OVER (ORDER BY ten), ten, four FROM tenk1 WHERE unique2 < 10;
|
2008-12-28 19:54:01 +01:00
|
|
|
|
|
|
|
SELECT last_value(ten) OVER (PARTITION BY four), ten, four FROM
|
|
|
|
(SELECT * FROM tenk1 WHERE unique2 < 10 ORDER BY four, ten)s
|
|
|
|
ORDER BY four, ten;
|
|
|
|
|
|
|
|
SELECT nth_value(ten, four + 1) OVER (PARTITION BY four), ten, four
|
|
|
|
FROM (SELECT * FROM tenk1 WHERE unique2 < 10 ORDER BY four, ten)s;
|
|
|
|
|
2010-11-23 21:27:50 +01:00
|
|
|
SELECT ten, two, sum(hundred) AS gsum, sum(sum(hundred)) OVER (PARTITION BY two ORDER BY ten) AS wsum
|
2008-12-28 19:54:01 +01:00
|
|
|
FROM tenk1 GROUP BY ten, two;
|
|
|
|
|
|
|
|
SELECT count(*) OVER (PARTITION BY four), four FROM (SELECT * FROM tenk1 WHERE two = 1)s WHERE unique2 < 10;
|
|
|
|
|
2010-11-23 21:27:50 +01:00
|
|
|
SELECT (count(*) OVER (PARTITION BY four ORDER BY ten) +
|
|
|
|
sum(hundred) OVER (PARTITION BY four ORDER BY ten))::varchar AS cntsum
|
2008-12-28 19:54:01 +01:00
|
|
|
FROM tenk1 WHERE unique2 < 10;
|
|
|
|
|
|
|
|
-- opexpr with different windows evaluation.
|
|
|
|
SELECT * FROM(
|
2010-11-23 21:27:50 +01:00
|
|
|
SELECT count(*) OVER (PARTITION BY four ORDER BY ten) +
|
|
|
|
sum(hundred) OVER (PARTITION BY two ORDER BY ten) AS total,
|
2008-12-28 19:54:01 +01:00
|
|
|
count(*) OVER (PARTITION BY four ORDER BY ten) AS fourcount,
|
|
|
|
sum(hundred) OVER (PARTITION BY two ORDER BY ten) AS twosum
|
|
|
|
FROM tenk1
|
|
|
|
)sub
|
|
|
|
WHERE total <> fourcount + twosum;
|
|
|
|
|
|
|
|
SELECT avg(four) OVER (PARTITION BY four ORDER BY thousand / 100) FROM tenk1 WHERE unique2 < 10;
|
|
|
|
|
2010-11-23 21:27:50 +01:00
|
|
|
SELECT ten, two, sum(hundred) AS gsum, sum(sum(hundred)) OVER win AS wsum
|
2008-12-28 19:54:01 +01:00
|
|
|
FROM tenk1 GROUP BY ten, two WINDOW win AS (PARTITION BY two ORDER BY ten);
|
|
|
|
|
|
|
|
-- more than one window with GROUP BY
|
|
|
|
SELECT sum(salary),
|
|
|
|
row_number() OVER (ORDER BY depname),
|
|
|
|
sum(sum(salary)) OVER (ORDER BY depname DESC)
|
|
|
|
FROM empsalary GROUP BY depname;
|
|
|
|
|
|
|
|
-- identical windows with different names
|
|
|
|
SELECT sum(salary) OVER w1, count(*) OVER w2
|
|
|
|
FROM empsalary WINDOW w1 AS (ORDER BY salary), w2 AS (ORDER BY salary);
|
|
|
|
|
|
|
|
-- subplan
|
|
|
|
SELECT lead(ten, (SELECT two FROM tenk1 WHERE s.unique2 = unique2)) OVER (PARTITION BY four ORDER BY ten)
|
|
|
|
FROM tenk1 s WHERE unique2 < 10;
|
|
|
|
|
|
|
|
-- empty table
|
|
|
|
SELECT count(*) OVER (PARTITION BY four) FROM (SELECT * FROM tenk1 WHERE FALSE)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 ORDER BY salary DESC);
|
|
|
|
|
|
|
|
-- strict aggs
|
|
|
|
SELECT empno, depname, salary, bonus, depadj, MIN(bonus) OVER (ORDER BY empno), MAX(depadj) OVER () FROM(
|
|
|
|
SELECT *,
|
|
|
|
CASE WHEN enroll_date < '2008-01-01' THEN 2008 - extract(YEAR FROM enroll_date) END * 500 AS bonus,
|
|
|
|
CASE WHEN
|
|
|
|
AVG(salary) OVER (PARTITION BY depname) < salary
|
|
|
|
THEN 200 END AS depadj FROM empsalary
|
|
|
|
)s;
|
|
|
|
|
Avoid listing ungrouped Vars in the targetlist of Agg-underneath-Window.
Regular aggregate functions in combination with, or within the arguments
of, window functions are OK per spec; they have the semantics that the
aggregate output rows are computed and then we run the window functions
over that row set. (Thus, this combination is not really useful unless
there's a GROUP BY so that more than one aggregate output row is possible.)
The case without GROUP BY could fail, as recently reported by Jeff Davis,
because sloppy construction of the Agg node's targetlist resulted in extra
references to possibly-ungrouped Vars appearing outside the aggregate
function calls themselves. See the added regression test case for an
example.
Fixing this requires modifying the API of flatten_tlist and its underlying
function pull_var_clause. I chose to make pull_var_clause's API for
aggregates identical to what it was already doing for placeholders, since
the useful behaviors turn out to be the same (error, report node as-is, or
recurse into it). I also tightened the error checking in this area a bit:
if it was ever valid to see an uplevel Var, Aggref, or PlaceHolderVar here,
that was a long time ago, so complain instead of ignoring them.
Backpatch into 9.1. The failure exists in 8.4 and 9.0 as well, but seeing
that it only occurs in a basically-useless corner case, it doesn't seem
worth the risks of changing a function API in a minor release. There might
be third-party code using pull_var_clause.
2011-07-13 00:23:55 +02:00
|
|
|
-- window function over ungrouped agg over empty row set (bug before 9.1)
|
|
|
|
SELECT SUM(COUNT(f1)) OVER () FROM int4_tbl WHERE f1=42;
|
|
|
|
|
Fix window functions that sort by expressions involving aggregates.
In commit c1d9579dd8bf3c921ca6bc2b62c40da6d25372e5, I changed things so
that the output of the Agg node that feeds the window functions would not
list any ungrouped Vars directly. Formerly, for example, the Agg tlist
might have included both "x" and "sum(x)", which is not really valid if
"x" isn't a grouping column. If we then had a window function ordering on
something like "sum(x) + 1", prepare_sort_from_pathkeys would find no exact
match for this in the Agg tlist, and would conclude that it must recompute
the expression. But it would break the expression down to just the Var
"x", which it would find in the tlist, and then rebuild the ORDER BY
expression using a reference to the subplan's "x" output. Now, after the
above-referenced changes, "x" isn't in the Agg tlist if it's not a grouping
column, so that prepare_sort_from_pathkeys fails with "could not find
pathkey item to sort", as reported by Bricklen Anderson.
The fix is to not break down Aggrefs into their component parts, but just
treat them as irreducible expressions to be sought in the subplan tlist.
This is definitely OK for the use with respect to window functions in
grouping_planner, since it just built the tlist being used on the same
basis. AFAICT it is safe for other uses too; most of the other call sites
couldn't encounter Aggrefs anyway.
2011-09-27 05:48:39 +02:00
|
|
|
-- window function with ORDER BY an expression involving aggregates (9.1 bug)
|
|
|
|
select ten,
|
|
|
|
sum(unique1) + sum(unique2) as res,
|
|
|
|
rank() over (order by sum(unique1) + sum(unique2)) as rank
|
|
|
|
from tenk1
|
|
|
|
group by ten order by ten;
|
|
|
|
|
2012-09-13 17:31:40 +02:00
|
|
|
-- 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
|
|
|
|
group by y;
|
|
|
|
|
2008-12-31 01:08:39 +01:00
|
|
|
-- test non-default frame specifications
|
|
|
|
SELECT four, ten,
|
|
|
|
sum(ten) over (partition by four order by ten),
|
|
|
|
last_value(ten) over (partition by four order by ten)
|
|
|
|
FROM (select distinct ten, four from tenk1) ss;
|
|
|
|
|
|
|
|
SELECT four, ten,
|
|
|
|
sum(ten) over (partition by four order by ten range between unbounded preceding and current row),
|
|
|
|
last_value(ten) over (partition by four order by ten range between unbounded preceding and current row)
|
|
|
|
FROM (select distinct ten, four from tenk1) ss;
|
|
|
|
|
|
|
|
SELECT four, ten,
|
|
|
|
sum(ten) over (partition by four order by ten range between unbounded preceding and unbounded following),
|
|
|
|
last_value(ten) over (partition by four order by ten range between unbounded preceding and unbounded following)
|
|
|
|
FROM (select distinct ten, four from tenk1) ss;
|
|
|
|
|
|
|
|
SELECT four, ten/4 as two,
|
|
|
|
sum(ten/4) over (partition by four order by ten/4 range between unbounded preceding and current row),
|
|
|
|
last_value(ten/4) over (partition by four order by ten/4 range between unbounded preceding and current row)
|
|
|
|
FROM (select distinct ten, four from tenk1) ss;
|
|
|
|
|
|
|
|
SELECT four, ten/4 as two,
|
|
|
|
sum(ten/4) over (partition by four order by ten/4 rows between unbounded preceding and current row),
|
|
|
|
last_value(ten/4) over (partition by four order by ten/4 rows between unbounded preceding and current row)
|
|
|
|
FROM (select distinct ten, four from tenk1) ss;
|
|
|
|
|
2010-02-12 18:33:21 +01:00
|
|
|
SELECT sum(unique1) over (order by 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 between 2 preceding and 2 following),
|
|
|
|
unique1, four
|
|
|
|
FROM tenk1 WHERE unique1 < 10;
|
|
|
|
|
|
|
|
SELECT sum(unique1) over (rows between 2 preceding and 1 preceding),
|
|
|
|
unique1, four
|
|
|
|
FROM tenk1 WHERE unique1 < 10;
|
|
|
|
|
|
|
|
SELECT sum(unique1) over (rows between 1 following and 3 following),
|
|
|
|
unique1, four
|
|
|
|
FROM tenk1 WHERE unique1 < 10;
|
|
|
|
|
|
|
|
SELECT sum(unique1) over (rows between unbounded preceding and 1 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 (order by four);
|
|
|
|
|
|
|
|
-- fail: not implemented yet
|
|
|
|
SELECT sum(unique1) over (order by four range between 2::int8 preceding and 1::int2 preceding),
|
|
|
|
unique1, four
|
|
|
|
FROM tenk1 WHERE unique1 < 10;
|
|
|
|
|
|
|
|
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 (order by four range between current row and unbounded following);
|
|
|
|
|
|
|
|
SELECT sum(unique1) over
|
2011-10-14 23:23:01 +02:00
|
|
|
(order by unique1
|
|
|
|
rows (SELECT unique1 FROM tenk1 ORDER BY unique1 LIMIT 1) + 1 PRECEDING),
|
2010-02-12 18:33:21 +01:00
|
|
|
unique1
|
|
|
|
FROM tenk1 WHERE unique1 < 10;
|
|
|
|
|
|
|
|
CREATE TEMP VIEW v_window AS
|
|
|
|
SELECT i, sum(i) over (order by i rows between 1 preceding and 1 following) as sum_rows
|
|
|
|
FROM generate_series(1, 10) i;
|
|
|
|
|
|
|
|
SELECT * FROM v_window;
|
|
|
|
|
|
|
|
SELECT pg_get_viewdef('v_window');
|
|
|
|
|
2008-12-28 19:54:01 +01:00
|
|
|
-- with UNION
|
|
|
|
SELECT count(*) OVER (PARTITION BY four) FROM (SELECT * FROM tenk1 UNION ALL SELECT * FROM tenk2)s LIMIT 0;
|
|
|
|
|
|
|
|
-- ordering by a non-integer constant is allowed
|
|
|
|
SELECT rank() OVER (ORDER BY length('abc'));
|
|
|
|
|
2009-08-27 22:08:03 +02:00
|
|
|
-- can't order by another window function
|
|
|
|
SELECT rank() OVER (ORDER BY rank() OVER (ORDER BY random()));
|
2008-12-28 19:54:01 +01:00
|
|
|
|
|
|
|
-- some other errors
|
|
|
|
SELECT * FROM empsalary WHERE row_number() OVER (ORDER BY salary) < 10;
|
|
|
|
|
|
|
|
SELECT * FROM empsalary INNER JOIN tenk1 ON row_number() OVER (ORDER BY salary) < 10;
|
|
|
|
|
|
|
|
SELECT rank() OVER (ORDER BY 1), count(*) FROM empsalary GROUP BY 1;
|
|
|
|
|
|
|
|
SELECT * FROM rank() OVER (ORDER BY random());
|
|
|
|
|
|
|
|
DELETE FROM empsalary WHERE (rank() OVER (ORDER BY random())) > 10;
|
|
|
|
|
|
|
|
DELETE FROM empsalary RETURNING rank() OVER (ORDER BY random());
|
|
|
|
|
|
|
|
SELECT count(*) OVER w FROM tenk1 WINDOW w AS (ORDER BY unique1), w AS (ORDER BY unique1);
|
|
|
|
|
|
|
|
SELECT rank() OVER (PARTITION BY four, ORDER BY ten) FROM tenk1;
|
|
|
|
|
|
|
|
SELECT count() OVER () FROM tenk1;
|
|
|
|
|
|
|
|
SELECT generate_series(1, 100) OVER () FROM empsalary;
|
|
|
|
|
|
|
|
SELECT ntile(0) OVER (ORDER BY ten), ten, four FROM tenk1;
|
|
|
|
|
|
|
|
SELECT nth_value(four, 0) OVER (ORDER BY ten), ten, four FROM tenk1;
|
|
|
|
|
|
|
|
-- cleanup
|
|
|
|
DROP TABLE empsalary;
|