Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d5816d1

Browse files
committed
Rewrite window function reference section to more clearly explain
keywords and concepts, based on suggestions by Florian Pflug.
1 parent 7c97f6a commit d5816d1

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

doc/src/sgml/syntax.sgml

+41-27
Original file line numberDiff line numberDiff line change
@@ -1742,11 +1742,16 @@ UNBOUNDED FOLLOWING
17421742
<para>
17431743
Here, <replaceable>expression</replaceable> represents any value
17441744
expression that does not itself contain window function calls.
1745-
The <literal>PARTITION BY</> and <literal>ORDER BY</> lists have
1746-
essentially the same syntax and semantics as <literal>GROUP BY</>
1747-
and <literal>ORDER BY</> clauses of the whole query, except that their
1745+
<literal>PARTITION BY</> behaves like a <literal>GROUP
1746+
BY</> clause by grouping rows into partitions, except its
17481747
expressions are always just expressions and cannot be output-column
1749-
names or numbers.
1748+
names or numbers. <literal>ORDER BY</> behaves similar to a
1749+
query-level <literal>ORDER BY</> clause, except it controls the
1750+
order rows are supplied to the aggregate function within the window
1751+
frame, and with the same expression restrictions.
1752+
</para>
1753+
1754+
<para>
17501755
<replaceable>window_name</replaceable> is a reference to a named window
17511756
specification defined in the query's <literal>WINDOW</literal> clause.
17521757
Named window specifications are usually referenced with just
@@ -1760,39 +1765,48 @@ UNBOUNDED FOLLOWING
17601765
page for details.
17611766
</para>
17621767

1768+
<para>
1769+
In <literal>RANGE</> mode, <literal>CURRENT ROW</> starts with
1770+
the current row's first peer that <literal>ORDER BY</>
1771+
considers equivalent, and ends with its last equivalent peer. In
1772+
<literal>ROWS</> mode, <literal>CURRENT ROW</> simply starts and ends
1773+
with the current row.
1774+
</para>
1775+
1776+
<para>
1777+
<literal>UNBOUNDED PRECEDING</> means that the frame
1778+
starts with the first row of the partition, and similarly
1779+
<literal>UNBOUNDED FOLLOWING</> means that the frame ends with the last
1780+
row of the partition.
1781+
The <replaceable>value</> <literal>PRECEDING</> and
1782+
<replaceable>value</> <literal>FOLLOWING</> cases are currently only
1783+
allowed in <literal>ROWS</> mode. They indicate that the frame starts
1784+
or ends the specified number of rows before or after the current row.
1785+
<replaceable>value</replaceable> must be an integer expression not
1786+
containing any variables, aggregate functions, or window functions.
1787+
The value must not be null or negative; but it can be zero, which
1788+
just selects the current row.
1789+
</para>
1790+
17631791
<para>
17641792
The <replaceable class="parameter">frame_clause</replaceable> specifies
17651793
the set of rows constituting the <firstterm>window frame</>, for those
17661794
window functions that act on the frame instead of the whole partition.
1767-
If <replaceable>frame_end</> is omitted it defaults to <literal>CURRENT
1768-
ROW</>. Restrictions are that
1795+
The default framing option is <literal>RANGE UNBOUNDED PRECEDING</>,
1796+
which is the same as <literal>RANGE BETWEEN UNBOUNDED PRECEDING AND
1797+
CURRENT ROW</>. This sets the frame to be all rows from the partition start
1798+
up through the current row's last peer as specified by <literal>ORDER BY</>,
1799+
or all partition rows if there is no <literal>ORDER BY</>.
1800+
</para>
1801+
1802+
<para>
1803+
Restrictions are that
17691804
<replaceable>frame_start</> cannot be <literal>UNBOUNDED FOLLOWING</>,
17701805
<replaceable>frame_end</> cannot be <literal>UNBOUNDED PRECEDING</>,
17711806
and the <replaceable>frame_end</> choice cannot appear earlier in the
17721807
above list than the <replaceable>frame_start</> choice &mdash; for example
17731808
<literal>RANGE BETWEEN CURRENT ROW AND <replaceable>value</>
17741809
PRECEDING</literal> is not allowed.
1775-
The default framing option is <literal>RANGE UNBOUNDED PRECEDING</>,
1776-
which is the same as <literal>RANGE BETWEEN UNBOUNDED PRECEDING AND
1777-
CURRENT ROW</>; it sets the frame to be all rows from the partition start
1778-
up through the current row's last peer in the <literal>ORDER BY</>
1779-
ordering (which means all rows if there is no <literal>ORDER BY</>).
1780-
In general, <literal>UNBOUNDED PRECEDING</> means that the frame
1781-
starts with the first row of the partition, and similarly
1782-
<literal>UNBOUNDED FOLLOWING</> means that the frame ends with the last
1783-
row of the partition (regardless of <literal>RANGE</> or <literal>ROWS</>
1784-
mode). In <literal>ROWS</> mode, <literal>CURRENT ROW</>
1785-
means that the frame starts or ends with the current row; but in
1786-
<literal>RANGE</> mode it means that the frame starts or ends with
1787-
the current row's first or last peer in the <literal>ORDER BY</> ordering.
1788-
The <replaceable>value</> <literal>PRECEDING</> and
1789-
<replaceable>value</> <literal>FOLLOWING</> cases are currently only
1790-
allowed in <literal>ROWS</> mode. They indicate that the frame starts
1791-
or ends with the row that many rows before or after the current row.
1792-
<replaceable>value</replaceable> must be an integer expression not
1793-
containing any variables, aggregate functions, or window functions.
1794-
The value must not be null or negative; but it can be zero, which
1795-
selects the current row itself.
17961810
</para>
17971811

17981812
<para>

0 commit comments

Comments
 (0)