Re: List Concatination

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Richard Huxton <dev(at)archonet(dot)com>
Cc: josh(at)agliodbs(dot)com, sqllist <pgsql-sql(at)postgresql(dot)org>
Subject: Re: List Concatination
Date: 2001-03-09 16:25:24
Message-ID: 6972.984155124@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Richard Huxton <dev(at)archonet(dot)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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2001-03-09 16:42:39 Re: from PosgreSQL 7.1b3 to 7.0.3
Previous Message Creager, Robert S 2001-03-09 16:21:32 cannot get CREATE TABLE AS to work