1
- <!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.457 2008/11/12 13:09:27 petere Exp $ -->
1
+ <!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.458 2008/11/13 15:59:50 petere Exp $ -->
2
2
3
3
<chapter id="functions">
4
4
<title>Functions and Operators</title>
@@ -8353,7 +8353,7 @@ SELECT xmlroot(xmlparse(document '<?xml version="1.1"?><content>abc</content>'),
8353
8353
</para>
8354
8354
</sect3>
8355
8355
8356
- <sect3>
8356
+ <sect3 id="functions-xml-xmlagg" >
8357
8357
<title><literal>xmlagg</literal></title>
8358
8358
8359
8359
<indexterm>
@@ -8366,10 +8366,10 @@ SELECT xmlroot(xmlparse(document '<?xml version="1.1"?><content>abc</content>'),
8366
8366
8367
8367
<para>
8368
8368
The function <function>xmlagg</function> is, unlike the other
8369
- functions below , an aggregate function. It concatenates the
8369
+ functions described here , an aggregate function. It concatenates the
8370
8370
input values to the aggregate function call,
8371
8371
like <function>xmlconcat</function> does.
8372
- See <xref linkend="functions-aggregate"> for general information
8372
+ See <xref linkend="functions-aggregate"> for additional information
8373
8373
about aggregate functions.
8374
8374
</para>
8375
8375
@@ -8387,23 +8387,18 @@ SELECT xmlagg(x) FROM test;
8387
8387
</para>
8388
8388
8389
8389
<para>
8390
- Note that in the current implementation, the order of the
8391
- concatenation is in principle undefined. Making the input values
8392
- to be sorted in some other way will usually work, however. For
8393
- instance, in the above example, one could influence the order
8394
- like so:
8390
+ The influence the order of the concatenation, something like the
8391
+ following approach to sort the input values can be used:
8392
+
8395
8393
<screen><![CDATA[
8396
8394
SELECT xmlagg(x) FROM (SELECT * FROM test ORDER BY y DESC) AS tab;
8397
8395
xmlagg
8398
8396
----------------------
8399
8397
<bar/><foo>abc</foo>
8400
8398
]]></screen>
8401
8399
8402
- But this approach is not guaranteed to work in all situations and
8403
- in all versions of PostgreSQL. A future version of PostgreSQL
8404
- will probably provide an additional feature to control the order
8405
- in a proper way (<literal>xmlagg(expr ORDER BY expr, expr,
8406
- ...</literal>).
8400
+ Again, see <xref linkend="functions-aggregate"> for additional
8401
+ information.
8407
8402
</para>
8408
8403
</sect3>
8409
8404
@@ -9490,6 +9485,11 @@ SELECT NULLIF(value, '(none)') ...
9490
9485
</tbody>
9491
9486
</tgroup>
9492
9487
</table>
9488
+
9489
+ <para>
9490
+ See also <xref linkend="functions-aggregate"> about the aggregate
9491
+ function <function>array_agg</function> for use with arrays.
9492
+ </para>
9493
9493
</sect1>
9494
9494
9495
9495
<sect1 id="functions-aggregate">
@@ -9526,6 +9526,22 @@ SELECT NULLIF(value, '(none)') ...
9526
9526
</thead>
9527
9527
9528
9528
<tbody>
9529
+ <row>
9530
+ <entry>
9531
+ <indexterm>
9532
+ <primary>array_agg</primary>
9533
+ </indexterm>
9534
+ <function>array_agg(<replaceable class="parameter">expression</replaceable>)</function>
9535
+ </entry>
9536
+ <entry>
9537
+ any
9538
+ </entry>
9539
+ <entry>
9540
+ array of the argument type
9541
+ </entry>
9542
+ <entry>input values concatenated into an array</entry>
9543
+ </row>
9544
+
9529
9545
<row>
9530
9546
<entry>
9531
9547
<indexterm>
@@ -9684,6 +9700,22 @@ SELECT NULLIF(value, '(none)') ...
9684
9700
</entry>
9685
9701
<entry>sum of <replaceable class="parameter">expression</replaceable> across all input values</entry>
9686
9702
</row>
9703
+
9704
+ <row>
9705
+ <entry>
9706
+ <indexterm>
9707
+ <primary>xmlagg</primary>
9708
+ </indexterm>
9709
+ <function>xmlagg(<replaceable class="parameter">expression</replaceable>)</function>
9710
+ </entry>
9711
+ <entry>
9712
+ <type>xml</type>
9713
+ </entry>
9714
+ <entry>
9715
+ <type>xml</type>
9716
+ </entry>
9717
+ <entry>concatenation of XML values (see also <xref linkend="functions-xml-xmlagg">)</entry>
9718
+ </row>
9687
9719
</tbody>
9688
9720
</tgroup>
9689
9721
</table>
@@ -9692,8 +9724,10 @@ SELECT NULLIF(value, '(none)') ...
9692
9724
It should be noted that except for <function>count</function>,
9693
9725
these functions return a null value when no rows are selected. In
9694
9726
particular, <function>sum</function> of no rows returns null, not
9695
- zero as one might expect. The <function>coalesce</function> function can be
9696
- used to substitute zero for null when necessary.
9727
+ zero as one might expect, and <function>array_agg</function>
9728
+ returns null rather than an empty array when there are no input
9729
+ rows. The <function>coalesce</function> function can be used to
9730
+ substitute zero or an empty array for null when necessary.
9697
9731
</para>
9698
9732
9699
9733
<note>
@@ -9733,6 +9767,25 @@ SELECT count(*) FROM sometable;
9733
9767
</para>
9734
9768
</note>
9735
9769
9770
+ <para>
9771
+ The aggregate functions <function>array_agg</function>
9772
+ and <function>xmlagg</function>, as well as similar user-defined
9773
+ aggregate functions, produce meaningfully different result values
9774
+ depending on the order of the input values. In the current
9775
+ implementation, the order of the concatenation is in principle
9776
+ undefined. Making the input values to be sorted in some other way
9777
+ will usually work, however. For example:
9778
+
9779
+ <screen><![CDATA[
9780
+ SELECT xmlagg(x) FROM (SELECT * FROM test ORDER BY y DESC) AS tab;
9781
+ ]]></screen>
9782
+
9783
+ But this approach is not guaranteed to work in all situations, and
9784
+ it is not strictly SQL-conforming. A future version of PostgreSQL
9785
+ might provide an additional feature to control the order in a
9786
+ better-defined way (<literal>xmlagg(expr ORDER BY expr, expr,
9787
+ ...</literal>).
9788
+ </para>
9736
9789
9737
9790
<para>
9738
9791
<xref linkend="functions-aggregate-statistics-table"> shows
0 commit comments