Home Explore Blog CI



postgresql

19th chunk of `doc/src/sgml/syntax.sgml`
36d58a3471fd3991fef968cab2af5b1e15e2b57aeea63a950000000100000fa4
 be added by the user.
   </para>

   <para>
    When issuing queries in a database where some users mistrust other users,
    observe security precautions from <xref linkend="typeconv-func"/> when
    writing function calls.
   </para>

   <para>
    The arguments can optionally have names attached.
    See <xref linkend="sql-syntax-calling-funcs"/> for details.
   </para>

   <note>
    <para>
     A function that takes a single argument of composite type can
     optionally be called using field-selection syntax, and conversely
     field selection can be written in functional style.  That is, the
     notations <literal>col(table)</literal> and <literal>table.col</literal> are
     interchangeable.  This behavior is not SQL-standard but is provided
     in <productname>PostgreSQL</productname> because it allows use of functions to
     emulate <quote>computed fields</quote>.  For more information see
     <xref linkend="rowtypes-usage"/>.
    </para>
   </note>
  </sect2>

  <sect2 id="syntax-aggregates">
   <title>Aggregate Expressions</title>

   <indexterm zone="syntax-aggregates">
    <primary>aggregate function</primary>
    <secondary>invocation</secondary>
   </indexterm>

   <indexterm zone="syntax-aggregates">
    <primary>ordered-set aggregate</primary>
   </indexterm>

   <indexterm zone="syntax-aggregates">
    <primary>WITHIN GROUP</primary>
   </indexterm>

   <indexterm zone="syntax-aggregates">
    <primary>FILTER</primary>
   </indexterm>

   <para>
    An <firstterm>aggregate expression</firstterm> represents the
    application of an aggregate function across the rows selected by a
    query.  An aggregate function reduces multiple inputs to a single
    output value, such as the sum or average of the inputs.  The
    syntax of an aggregate expression is one of the following:

<synopsis>
<replaceable>aggregate_name</replaceable> (<replaceable>expression</replaceable> [ , ... ] [ <replaceable>order_by_clause</replaceable> ] ) [ FILTER ( WHERE <replaceable>filter_clause</replaceable> ) ]
<replaceable>aggregate_name</replaceable> (ALL <replaceable>expression</replaceable> [ , ... ] [ <replaceable>order_by_clause</replaceable> ] ) [ FILTER ( WHERE <replaceable>filter_clause</replaceable> ) ]
<replaceable>aggregate_name</replaceable> (DISTINCT <replaceable>expression</replaceable> [ , ... ] [ <replaceable>order_by_clause</replaceable> ] ) [ FILTER ( WHERE <replaceable>filter_clause</replaceable> ) ]
<replaceable>aggregate_name</replaceable> ( * ) [ FILTER ( WHERE <replaceable>filter_clause</replaceable> ) ]
<replaceable>aggregate_name</replaceable> ( [ <replaceable>expression</replaceable> [ , ... ] ] ) WITHIN GROUP ( <replaceable>order_by_clause</replaceable> ) [ FILTER ( WHERE <replaceable>filter_clause</replaceable> ) ]
</synopsis>

    where <replaceable>aggregate_name</replaceable> is a previously
    defined aggregate (possibly qualified with a schema name) and
    <replaceable>expression</replaceable> is
    any value expression that does not itself contain an aggregate
    expression or a window function call.  The optional
    <replaceable>order_by_clause</replaceable> and
    <replaceable>filter_clause</replaceable> are described below.
   </para>

   <para>
    The first form of aggregate expression invokes the aggregate
    once for each input row.
    The second form is the same as the first, since
    <literal>ALL</literal> is the default.
    The third form invokes the aggregate once for each distinct value
    of the expression (or distinct set of values, for multiple expressions)
    found in the input rows.
    The fourth form invokes the aggregate once for each input row; since no
    particular input value is specified, it is generally only useful
    for the <function>count(*)</function> aggregate function.
    The last form is used with <firstterm>ordered-set</firstterm> aggregate
    functions, which are described below.
   </para>

   <para>
    Most aggregate functions ignore null inputs,

Title: Aggregate Expressions
Summary
This section describes aggregate expressions, which apply aggregate functions to selected rows in a query to produce a single output value. It details the syntax of aggregate expressions, including variations for different input types (ALL, DISTINCT, *) and the use of WITHIN GROUP for ordered-set aggregates, as well as FILTER clauses. It notes that aggregate functions generally ignore null inputs.