Re: Join field values

From: Erwin Brandstetter <brsaweda(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Join field values
Date: 2007-06-05 20:14:19
Message-ID: 1181074459.104176.121520@q75g2000hsh.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Jun 5, 7:39 pm, g(dot)(dot)(dot)(at)hive(dot)is (Ragnar) wrote:
>
> create an aggregate function and use that in your select.http://www.postgresql.org/docs/8.2/static/sql-createaggregate.html

Of course you could do that. And it would look like that:

CREATE OR REPLACE FUNCTION f_concat_comma(text, text)
RETURNS text AS
$BODY$
BEGIN
RETURN $1 || ', ' || $2;
END;
$BODY$
LANGUAGE 'plpgsql' STABLE IMMUTABLE;

CREATE AGGREGATE concat_comma(
BASETYPE=text,
SFUNC=f_concat_comma,
STYPE=text
);

SELECT concat_comma(field1) FROM mytbl;

--Or, if want the values ordered:
SELECT concat_comma(field1) FROM (SELECT field1 FROM mytbl ORDER by
field1) x;

And that's what I did - before I found out about this simpler way:

SELECT array_to_string(ARRAY(SELECT field1 FROM mytbl ORDER by
field1), ', ');

More info:
http://www.postgresql.org/docs/8.2/static/functions-array.html

Regards
Erwin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Joris Dobbelsteen 2007-06-05 20:35:28 Re: Creditcard Number Security was Re: Encrypted column
Previous Message Pavel Stehule 2007-06-05 20:01:49 Re: pl/pgsql debuging, was Re: debugging C functions