Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit72150db

Browse files
committed
Add an example to clarify the use of ORDER BY in multiple-argument
aggregates. People seem to not get this right without help.
1 parent1453435 commit72150db

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

‎doc/src/sgml/syntax.sgml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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 $ -->
22

33
<chapter id="sql-syntax">
44
<title>SQL Syntax</title>
@@ -1567,20 +1567,34 @@ sqrt(2)
15671567
unspecified order. In many cases this does not matter; for example,
15681568
<function>min</> produces the same result no matter what order it
15691569
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
15711571
results that depend on the ordering of the input rows. When using
15721572
such an aggregate, the optional <replaceable>order_by_clause</> can be
15731573
used to specify the desired ordering. The <replaceable>order_by_clause</>
15741574
has the same syntax as for a query-level <literal>ORDER BY</> clause, as
15751575
described in <xref linkend="queries-order">, except that its expressions
15761576
are always just expressions and cannot be output-column names or numbers.
15771577
For example:
1578-
15791578
<programlisting>
15801579
SELECT array_agg(a ORDER BY b DESC) FROM table;
15811580
</programlisting>
15821581
</para>
15831582

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+
15841598
<para>
15851599
If <literal>DISTINCT</> is specified in addition to an
15861600
<replaceable>order_by_clause</>, then all the <literal>ORDER BY</>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp