From: Tom Lane <tgl@sss.pgh.pa.us>
To: Richard Huxton <dev@archonet.com>
Cc: josh@agliodbs.com, sqllist <pgsql-sql@postgresql.org>
Subject: Re: List Concatination
Date: Fri, 09 Mar 2001 11:25:24 -0500
Message-ID: <6972.984155124@sss.pgh.pa.us> (raw)
In-Reply-To: <3AA8BB73.5647B248@archonet.com>
References: <3AA86767.698DDFFF@agliodbs.com>
<3AA8BB73.5647B248@archonet.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, dev@archonet.com
Subject: Re: List Concatination
In-Reply-To: <6972.984155124@sss.pgh.pa.us>
References: <3AA86767.698DDFFF@agliodbs.com> <3AA8BB73.5647B248@archonet.com> <6972.984155124@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox