*** pgsql/doc/src/sgml/ref/select.sgml 2008/12/01 09:38:08 1.112 --- pgsql/doc/src/sgml/ref/select.sgml 2008/12/28 18:53:54 1.113 *************** *** 1,5 **** --- 1,5 ---- *************** SELECT [ ALL | DISTINCT [ ON ( condition ] [ GROUP BY expression [, ...] ] [ HAVING condition [, ...] ] + [ WINDOW window_name AS ( window_definition ) [, ...] ] [ { UNION | INTERSECT | EXCEPT } [ ALL ] select ] [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ] [ LIMIT { count | ALL } ] *************** HAVING co *** 566,571 **** --- 567,633 ---- + + <literal>WINDOW</literal> Clause + + + The optional WINDOW clause has the general form + + WINDOW window_name AS ( window_definition ) [, ...] + + where window_name is + a name that can be referenced from subsequent window definitions or + OVER clauses, and + window_definition is + + [ existing_window_name ] + [ PARTITION BY expression [, ...] ] + [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ] + + The elements of the PARTITION BY list are interpreted in + the same fashion as elements of a + , and + the elements of the ORDER BY list are interpreted in the + same fashion as elements of an + . + The only difference is that these expressions can contain aggregate + function calls, which are not allowed in a regular GROUP BY + clause. They are allowed here because windowing occurs after grouping + and aggregation. + + + + If an existing_window_name + is specified it must refer to an earlier entry in the WINDOW + list; the new window copies its partitioning clause from that entry, + as well as its ordering clause if any. In this case the new window cannot + specify its own PARTITION BY clause, and it can specify + ORDER BY only if the copied window does not have one. + + + + The purpose of a WINDOW clause is to specify the + behavior of window functions appearing in the query's + or + . These functions + can reference the WINDOW clause entries by name + in their OVER clauses. A WINDOW clause + entry does not have to be referenced anywhere, however; if it is not + used in the query it is simply ignored. It is possible to use window + functions without any WINDOW clause at all, since + a window function call can specify its window definition directly in + its OVER clause. However, the WINDOW + clause saves typing when the same window definition is needed for more + than one window function. + + + + Window functions are described in detail in + and + . + + + <command>SELECT</command> List *************** FETCH { FIRST | NEXT } [ When using LIMIT, it is a good idea to use an --- 984,990 ---- constants for the offset or fetch count, parentheses will be necessary in most cases. If the fetch count is omitted, it defaults to 1. ! When using LIMIT, it is a good idea to use an *************** SELECT distributors.* WHERE distributors *** 1387,1392 **** --- 1449,1467 ---- + + <literal>WINDOW</literal> Clause Restrictions + + + The SQL standard provides for an optional framing clause, + introduced by the key word RANGE or ROWS, + in window definitions. PostgreSQL does + not yet implement framing clauses, and always follows the + default framing behavior, which is equivalent to the framing clause + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. + + + <literal>LIMIT</literal> and <literal>OFFSET</literal>