Re: Aggregates, group, and order by

From: Joe Conway <mail(at)joeconway(dot)com>
To: Michael Glaesemann <grzm(at)myrealbox(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Aggregates, group, and order by
Date: 2005-11-07 09:09:19
Message-ID: 436F19BF.3050401@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Michael Glaesemann wrote:
> I'm trying to concatenate strings in variable orders using a custom
> aggregate. However, I'm having a difficult time figuring out the SQL I
> need to use to accomplish this. Here's a test case that shows the error
> I'm getting.

> select bar_id, array_accum(foo_value)
> from ordered_foo
> group by bar_id
> order by bar_id;
> bar_id | array_accum
> --------+-----------------------------
> 1 | {delta,alpha,charlie,bravo}
> 2 | {C,B,A,D}
>
>
> The result I'd like to see is
> bar_id | array_accum
> --------+-----------------------------
> 1 | {alpha,bravo,charlie,delta}
> 2 | {A,B,C,D}

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;
bar_id | array_accum
--------+-----------------------------
1 | {alpha,bravo,charlie,delta}
2 | {A,B,C,D}
(2 rows)

HTH,

Joe

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Mariusz Czułada 2005-11-07 09:17:31 Re: Oracle 10g Express - any danger for Postgres?
Previous Message David Fetter 2005-11-07 08:47:54 Re: Aggregates, group, and order by