Re: List Concatination

From: Josh Berkus <josh(at)agliodbs(dot)com>
To:
Cc: sqllist <pgsql-sql(at)postgresql(dot)org>
Subject: Re: List Concatination
Date: 2001-03-15 16:29:50
Message-ID: 3AB0EDFE.998C387C@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Richard,

I wanted to thank you for the concatination suggestion ... on testing,
a custom aggregate *was* faster than procedural concatination ... much
faster.

> But - if you don't care about the order of contacts you can define an
> aggregate function:
>
> create aggregate catenate(sfunc1=textcat, basetype=text, stype1=text, initcond1='');
>
> Then group by client and catenate(firstname || ' ' || lastname)
>
> You'll want to read the CREATE AGGREGATE page in the reference manual,
> replace textcat with your own routine that adds a comma and you'll need
> a finalisation routine to strip the final trailing comma.

Actually, if you use a sub-select as your data source, you can control
both the appearance and the order of the catenated values:

SELECT client, catenate(con_name)
FROM (SELECT client, (firstname || ' ' || lastname || ', '
FROM contacts ORDER BY lastname ASC) AS con_list
GROUP BY client;

This seems to work pretty well.

> Note that this is probably not a good idea - the ordering of the
> contacts will not be well-defined. When I asked about this Tom Lane was
> quite surprised that it worked, so no guarantees about long-term suitability.

Hmmm ... this feature is very, very, useful now that I know how to use
it. I'd love to see it hang around for future versions of PgSQL. Tom?

-Josh Berkus

--
______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2001-03-15 16:35:28 Re: Some questions about PLpgSql
Previous Message Josh Berkus 2001-03-15 16:23:50 Re: Some questions about PLpgSql