Re: List Concatination - Mailing list pgsql-sql

From Josh Berkus
Subject Re: List Concatination
Date
Msg-id 3AB0EDFE.998C387C@agliodbs.com
Whole thread Raw
In response to List Concatination  (Josh Berkus <josh@agliodbs.com>)
Responses Re: List Concatination
List 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
informationtechnology      josh@agliodbs.com   and data management solutions       (415) 565-7293  for law firms, small
businesses      fax  621-2533   and non-profit organizations.       San Francisco
 


pgsql-sql by date:

Previous
From: Josh Berkus
Date:
Subject: Re: Some questions about PLpgSql
Next
From: Tom Lane
Date:
Subject: Re: Some questions about PLpgSql