Re: How do I aggregate data from multiple rows into a delimited list?

From: Gunzler <geoff(dot)kloess(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: How do I aggregate data from multiple rows into a delimited list?
Date: 2007-07-03 23:50:55
Message-ID: 1183506655.311805.253130@i38g2000prf.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Jul 1, 3:09 pm, d(dot)(dot)(dot)(at)larkspark(dot)com ("D. Dante Lorenso") wrote:
> I want to select several rows of data and have them returned in a single
> record with the rows joined by a delimiter. It would be great if this
> could be done in a generic way possibly using the GROUP BY like this:

I recently wrote this to address the same need. I practically copied
it straight from the "CREATE AGGREGATE" documentation, so look there
for more information. It will create a "text_accumulate" aggregate
function that does what you need:

CREATE FUNCTION text_append(text,text) RETURNS text
AS 'select $1 || '','' || $2;'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;

CREATE AGGREGATE text_accumulate(
BASETYPE = text,
SFUNC = text_append,
STYPE = text
);

Geoff

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Clemens Schwaighofer 2007-07-04 00:36:48 bad function lets postgres crash
Previous Message Raymond O'Donnell 2007-07-03 22:37:57 Mugs 'n stuff