Home Explore Blog CI



postgresql

24th chunk of `doc/src/sgml/syntax.sgml`
15ffc3c0f60e8de1127e2638921ca456fffae496755c81930000000100000fb5
 <replaceable>frame_exclusion</replaceable> can be one of
<synopsis>
EXCLUDE CURRENT ROW
EXCLUDE GROUP
EXCLUDE TIES
EXCLUDE NO OTHERS
</synopsis>
   </para>

   <para>
    Here, <replaceable>expression</replaceable> represents any value
    expression that does not itself contain window function calls.
   </para>

   <para>
    <replaceable>window_name</replaceable> is a reference to a named window
    specification defined in the query's <literal>WINDOW</literal> clause.
    Alternatively, a full <replaceable>window_definition</replaceable> can
    be given within parentheses, using the same syntax as for defining a
    named window in the <literal>WINDOW</literal> clause; see the
    <xref linkend="sql-select"/> reference page for details.  It's worth
    pointing out that <literal>OVER wname</literal> is not exactly equivalent to
    <literal>OVER (wname ...)</literal>; the latter implies copying and modifying the
    window definition, and will be rejected if the referenced window
    specification includes a frame clause.
   </para>

   <para>
    The <literal>PARTITION BY</literal> clause groups the rows of the query into
    <firstterm>partitions</firstterm>, which are processed separately by the window
    function.  <literal>PARTITION BY</literal> works similarly to a query-level
    <literal>GROUP BY</literal> clause, except that its expressions are always just
    expressions and cannot be output-column names or numbers.
    Without <literal>PARTITION BY</literal>, all rows produced by the query are
    treated as a single partition.
    The <literal>ORDER BY</literal> clause determines the order in which the rows
    of a partition are processed by the window function.  It works similarly
    to a query-level <literal>ORDER BY</literal> clause, but likewise cannot use
    output-column names or numbers.  Without <literal>ORDER BY</literal>, rows are
    processed in an unspecified order.
   </para>

   <para>
    The <replaceable class="parameter">frame_clause</replaceable> specifies
    the set of rows constituting the <firstterm>window frame</firstterm>, which is a
    subset of the current partition, for those window functions that act on
    the frame instead of the whole partition.  The set of rows in the frame
    can vary depending on which row is the current row.  The frame can be
    specified in <literal>RANGE</literal>, <literal>ROWS</literal>
    or <literal>GROUPS</literal> mode; in each case, it runs from
    the <replaceable>frame_start</replaceable> to
    the <replaceable>frame_end</replaceable>.
    If <replaceable>frame_end</replaceable> is omitted, the end defaults
    to <literal>CURRENT ROW</literal>.
   </para>

   <para>
    A <replaceable>frame_start</replaceable> of <literal>UNBOUNDED PRECEDING</literal> means
    that the frame starts with the first row of the partition, and similarly
    a <replaceable>frame_end</replaceable> of <literal>UNBOUNDED FOLLOWING</literal> means
    that the frame ends with the last row of the partition.
   </para>

   <para>
    In <literal>RANGE</literal> or <literal>GROUPS</literal> mode,
    a <replaceable>frame_start</replaceable> of
    <literal>CURRENT ROW</literal> means the frame starts with the current
    row's first <firstterm>peer</firstterm> row (a row that the
    window's <literal>ORDER BY</literal> clause sorts as equivalent to the
    current row), while a <replaceable>frame_end</replaceable> of
    <literal>CURRENT ROW</literal> means the frame ends with the current
    row's last peer row.
    In <literal>ROWS</literal> mode, <literal>CURRENT ROW</literal> simply
    means the current row.
   </para>

   <para>
    In the <replaceable>offset</replaceable> <literal>PRECEDING</literal>
    and <replaceable>offset</replaceable> <literal>FOLLOWING</literal> frame
    options, the <replaceable>offset</replaceable> must be an expression not
    containing any variables, aggregate functions, or window functions.
    The meaning of the <replaceable>offset</replaceable>

Title: Window Function Details: Frame Exclusion, Partitioning, Ordering, and Frame Specification
Summary
This section continues detailing window functions, covering frame exclusion options (EXCLUDE CURRENT ROW, GROUP, TIES, NO OTHERS) and clarifying that expressions within window function calls cannot contain other window function calls. It explains the function of the PARTITION BY clause, which divides rows into partitions similar to GROUP BY, and the ORDER BY clause, which determines processing order within partitions. It describes how the frame_clause defines the window frame using RANGE, ROWS, or GROUPS modes, from frame_start to frame_end, with defaults and options like UNBOUNDED PRECEDING/FOLLOWING and CURRENT ROW. It also specifies that offsets in PRECEDING/FOLLOWING options must be expressions without variables, aggregates, or window functions.