From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Joe Conway <mail(at)joeconway(dot)com> |
Cc: | Michael Glaesemann <grzm(at)myrealbox(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Aggregates, group, and order by |
Date: | 2005-11-07 14:24:08 |
Message-ID: | 18118.1131373448@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Joe Conway <mail(at)joeconway(dot)com> writes:
> Michael Glaesemann wrote:
>> I'm trying to concatenate strings in variable orders using a custom
>> aggregate.
> Just use a subselect -- you're looking for this, correct?
> regression=# select bar_id, array_accum(foo_value) from (select * from
> ordered_foo order by foo_pos) as ss group by bar_id order by bar_id;
Strictly speaking, you need this:
select bar_id, array_accum(foo_value) from
(select * from ordered_foo order by bar_id, foo_pos) as ss
group by bar_id order by bar_id;
ie, sort the subselect by the grouping key of the outer query, then
by the thing that should control the aggregation order within groups.
The way Joe shows will work only if the planner chooses to use a hash
aggregate plan. If it chooses a sort/uniq aggregation plan, the re-sort
will destroy the sort order of the sub-select's output.
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | Michael Glaesemann | 2005-11-07 14:38:54 | Re: Aggregates, group, and order by |
Previous Message | Guido Neitzer | 2005-11-07 13:28:05 | Re: PostgreSQL, UTF-8 and Mac OS X |