@@ -1554,6 +1554,10 @@ sqrt(2)
15541554 <secondary>invocation</secondary>
15551555 </indexterm>
15561556
1557+ <indexterm zone="syntax-aggregates">
1558+ <primary>filter</primary>
1559+ </indexterm>
1560+
15571561 <para>
15581562 An <firstterm>aggregate expression</firstterm> represents the
15591563 application of an aggregate function across the rows selected by a
@@ -1562,19 +1566,19 @@ sqrt(2)
15621566 syntax of an aggregate expression is one of the following:
15631567
15641568<synopsis>
1565- <replaceable>aggregate_name</replaceable> (<replaceable>expression</replaceable> [ , ... ] [ <replaceable>order_by_clause</replaceable> ] )
1566- <replaceable>aggregate_name</replaceable> (ALL <replaceable>expression</replaceable> [ , ... ] [ <replaceable>order_by_clause</replaceable> ] )
1567- <replaceable>aggregate_name</replaceable> (DISTINCT <replaceable>expression</replaceable> [ , ... ] [ <replaceable>order_by_clause</replaceable> ] )
1568- <replaceable>aggregate_name</replaceable> ( * )
1569+ <replaceable>aggregate_name</replaceable> (<replaceable>expression</replaceable> [ , ... ] [ <replaceable>order_by_clause</replaceable> ] ) [ FILTER ( WHERE <replaceable>filter_clause</replaceable> ) ]
1570+ <replaceable>aggregate_name</replaceable> (ALL <replaceable>expression</replaceable> [ , ... ] [ <replaceable>order_by_clause</replaceable> ] ) [ FILTER ( WHERE <replaceable>filter_clause</replaceable> ) ]
1571+ <replaceable>aggregate_name</replaceable> (DISTINCT <replaceable>expression</replaceable> [ , ... ] [ <replaceable>order_by_clause</replaceable> ] ) [ FILTER ( WHERE <replaceable>filter_clause</replaceable> ) ]
1572+ <replaceable>aggregate_name</replaceable> ( * ) [ FILTER ( WHERE <replaceable>filter_clause</replaceable> ) ]
15691573</synopsis>
15701574
15711575 where <replaceable>aggregate_name</replaceable> is a previously
1572- defined aggregate (possibly qualified with a schema name),
1576+ defined aggregate (possibly qualified with a schema name) and
15731577 <replaceable>expression</replaceable> is
15741578 any value expression that does not itself contain an aggregate
1575- expression or a window function call, and
1576- <replaceable>order_by_clause</replaceable> is a optional
1577- <literal>ORDER BY</> clause as described below.
1579+ expression or a window function call. The optional
1580+ <replaceable>order_by_clause</replaceable> and
1581+ <replaceable>filter_clause</replaceable> are described below.
15781582 </para>
15791583
15801584 <para>
@@ -1606,6 +1610,23 @@ sqrt(2)
16061610 distinct non-null values of <literal>f1</literal>.
16071611 </para>
16081612
1613+ <para>
1614+ If <literal>FILTER</literal> is specified, then only the input
1615+ rows for which the <replaceable>filter_clause</replaceable>
1616+ evaluates to true are fed to the aggregate function; other rows
1617+ are discarded. For example:
1618+ <programlisting>
1619+ SELECT
1620+ count(*) AS unfiltered,
1621+ count(*) FILTER (WHERE i < 5) AS filtered
1622+ FROM generate_series(1,10) AS s(i);
1623+ unfiltered | filtered
1624+ ------------+----------
1625+ 10 | 4
1626+ (1 row)
1627+ </programlisting>
1628+ </para>
1629+
16091630 <para>
16101631 Ordinarily, the input rows are fed to the aggregate function in an
16111632 unspecified order. In many cases this does not matter; for example,
@@ -1709,10 +1730,10 @@ SELECT string_agg(a ORDER BY a, ',') FROM table; -- incorrect
17091730 The syntax of a window function call is one of the following:
17101731
17111732<synopsis>
1712- <replaceable>function_name</replaceable> (<optional><replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> ... </optional></optional>) OVER ( <replaceable class="parameter">window_definition</replaceable> )
1713- <replaceable>function_name</replaceable> (<optional><replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> ... </optional></optional>) OVER <replaceable>window_name</replaceable>
1714- <replaceable>function_name</replaceable> ( * ) OVER ( <replaceable class="parameter">window_definition</replaceable> )
1715- <replaceable>function_name</replaceable> ( * ) OVER <replaceable>window_name</replaceable>
1733+ <replaceable>function_name</replaceable> (<optional><replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> ... </optional></optional>) [ FILTER ( WHERE <replaceable>filter_clause</replaceable> ) ] OVER ( <replaceable class="parameter">window_definition</replaceable> )
1734+ <replaceable>function_name</replaceable> (<optional><replaceable>expression</replaceable> <optional>, <replaceable>expression</replaceable> ... </optional></optional>) [ FILTER ( WHERE <replaceable>filter_clause</replaceable> ) ] OVER <replaceable>window_name</replaceable>
1735+ <replaceable>function_name</replaceable> ( * ) [ FILTER ( WHERE <replaceable>filter_clause</replaceable> ) ] OVER ( <replaceable class="parameter">window_definition</replaceable> )
1736+ <replaceable>function_name</replaceable> ( * ) [ FILTER ( WHERE <replaceable>filter_clause</replaceable> ) ] OVER <replaceable>window_name</replaceable>
17161737</synopsis>
17171738 where <replaceable class="parameter">window_definition</replaceable>
17181739 has the syntax
@@ -1836,7 +1857,8 @@ UNBOUNDED FOLLOWING
18361857 The built-in window functions are described in <xref
18371858 linkend="functions-window-table">. Other window functions can be added by
18381859 the user. Also, any built-in or user-defined aggregate function can be
1839- used as a window function.
1860+ used as a window function. Only aggregate window functions accept
1861+ a <literal>FILTER</literal> clause.
18401862 </para>
18411863
18421864 <para>
0 commit comments