Re: string_agg delimiter having no effect with order by - Mailing list pgsql-bugs

From Tom Lane
Subject Re: string_agg delimiter having no effect with order by
Date
Msg-id 19470.1280935779@sss.pgh.pa.us
Whole thread Raw
In response to Re: string_agg delimiter having no effect with order by  (Robert Haas <robertmhaas@gmail.com>)
Responses Re: string_agg delimiter having no effect with order by  (Robert Haas <robertmhaas@gmail.com>)
List pgsql-bugs
Robert Haas <robertmhaas@gmail.com> writes:
> Oh, yeah.  I guess you need this:

> select thing, string_agg(stuff, ',' order by stuff) from agg_test
> group by thing;

> Rather than this:

> select thing, string_agg(stuff order by stuff, ',') from agg_test
> group by thing;

> It's all kinds of not obvious to me what the second one is supposed to
> mean, but I remember this was discussed before.  Perhaps we need a
> <note> somewhere about multi-argument aggregates.

Done:

+    <para>
+     When dealing with multiple-argument aggregate functions, note that the
+     <literal>ORDER BY</> clause goes after all the aggregate arguments.
+     For example, this:
+ <programlisting>
+ SELECT string_agg(a, ',' ORDER BY a) FROM table;
+ </programlisting>
+     not this:
+ <programlisting>
+ SELECT string_agg(a ORDER BY a, ',') FROM table;  -- not what you want
+ </programlisting>
+     The latter syntax will be accepted, but <literal>','</> will be
+     treated as a (useless) sort key.
+    </para>

            regards, tom lane

pgsql-bugs by date:

Previous
From: "Shine"
Date:
Subject: BUG #5598: Compatibility modes
Next
From: Tom Lane
Date:
Subject: Re: In 8.2, shutdown wrongly caused automatic restart