Received: from sss.pgh.pa.us (sss.pgh.pa.us [209.114.132.154]) by mail.postgresql.org (8.11.1/8.11.1) with ESMTP id f29GPVx79052 for ; Fri, 9 Mar 2001 11:25:31 -0500 (EST) (envelope-from tgl@sss.pgh.pa.us) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.11.1/8.11.1) with ESMTP id f29GPO906975; Fri, 9 Mar 2001 11:25:24 -0500 (EST) To: Richard Huxton cc: josh@agliodbs.com, sqllist Subject: Re: List Concatination In-reply-to: <3AA8BB73.5647B248@archonet.com> References: <3AA86767.698DDFFF@agliodbs.com> <3AA8BB73.5647B248@archonet.com> Comments: In-reply-to Richard Huxton message dated "Fri, 09 Mar 2001 11:16:04 +0000" Date: Fri, 09 Mar 2001 11:25:24 -0500 Message-ID: <6972.984155124@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200103/193 X-Sequence-Number: 1846 Richard Huxton 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