|
1 |
| -<!-- $PostgreSQL: pgsql/doc/src/sgml/syntax.sgml,v 1.148 2010/07/20 00:34:44 rhaas Exp $ --> |
| 1 | +<!-- $PostgreSQL: pgsql/doc/src/sgml/syntax.sgml,v 1.149 2010/08/04 15:27:57 tgl Exp $ --> |
2 | 2 |
|
3 | 3 | <chapter id="sql-syntax">
|
4 | 4 | <title>SQL Syntax</title>
|
@@ -1567,20 +1567,34 @@ sqrt(2)
|
1567 | 1567 | unspecified order. In many cases this does not matter; for example,
|
1568 | 1568 | <function>min</> produces the same result no matter what order it
|
1569 | 1569 | receives the inputs in. However, some aggregate functions
|
1570 |
| - (such as <function>array_agg</> and <function>xmlagg</>) produce |
| 1570 | + (such as <function>array_agg</> and <function>string_agg</>) produce |
1571 | 1571 | results that depend on the ordering of the input rows. When using
|
1572 | 1572 | such an aggregate, the optional <replaceable>order_by_clause</> can be
|
1573 | 1573 | used to specify the desired ordering. The <replaceable>order_by_clause</>
|
1574 | 1574 | has the same syntax as for a query-level <literal>ORDER BY</> clause, as
|
1575 | 1575 | described in <xref linkend="queries-order">, except that its expressions
|
1576 | 1576 | are always just expressions and cannot be output-column names or numbers.
|
1577 | 1577 | For example:
|
1578 |
| - |
1579 | 1578 | <programlisting>
|
1580 | 1579 | SELECT array_agg(a ORDER BY b DESC) FROM table;
|
1581 | 1580 | </programlisting>
|
1582 | 1581 | </para>
|
1583 | 1582 |
|
| 1583 | + <para> |
| 1584 | + When dealing with multiple-argument aggregate functions, note that the |
| 1585 | + <literal>ORDER BY</> clause goes after all the aggregate arguments. |
| 1586 | + For example, this: |
| 1587 | +<programlisting> |
| 1588 | +SELECT string_agg(a, ',' ORDER BY a) FROM table; |
| 1589 | +</programlisting> |
| 1590 | + not this: |
| 1591 | +<programlisting> |
| 1592 | +SELECT string_agg(a ORDER BY a, ',') FROM table; -- not what you want |
| 1593 | +</programlisting> |
| 1594 | + The latter syntax will be accepted, but <literal>','</> will be |
| 1595 | + treated as a (useless) sort key. |
| 1596 | + </para> |
| 1597 | + |
1584 | 1598 | <para>
|
1585 | 1599 | If <literal>DISTINCT</> is specified in addition to an
|
1586 | 1600 | <replaceable>order_by_clause</>, then all the <literal>ORDER BY</>
|
|