Re: List Concatination - Mailing list pgsql-sql

From Tom Lane
Subject Re: List Concatination
Date
Msg-id 6972.984155124@sss.pgh.pa.us
Whole thread Raw
In response to Re: List Concatination  (Richard Huxton <dev@archonet.com>)
Responses Re: List Concatination  ("Josh Berkus" <josh@agliodbs.com>)
List pgsql-sql
Richard Huxton <dev@archonet.com> writes:
> 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)

With a custom aggregate you could make the aggregate function
responsible for handling the ordering of contacts:
select client, contactlist(contact) from table group by client;

If I were doing this, I'd make the aggregate state variable be "array of
text", and have the transition function simply append each new value to
the array.  (Or, if you're willing to assume that no newlines appear in
the contact names, the state variable can be plain text and can list the
contacts one per line.)  Then the finalization function would sort the
array elements and concatenate them with inserted commas.  These two
functions would be pretty trivial to write in pltcl or plperl, either
of which are the tool of first choice for string-bashing problems.

This wouldn't scale very well to huge numbers of contacts per client,
but for the numbers that would be reasonable to print out as single
lines of a report it should work fine.

> 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.

I don't recall the prior conversation, but certainly user-defined
aggregates are not going away...
        regards, tom lane


pgsql-sql by date:

Previous
From: "Creager, Robert S"
Date:
Subject: cannot get CREATE TABLE AS to work
Next
From: Tom Lane
Date:
Subject: Re: from PosgreSQL 7.1b3 to 7.0.3