X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 0FF9753804 for ; Sun, 8 May 2005 18:48:44 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 55119-08 for ; Sun, 8 May 2005 21:48:41 +0000 (GMT) Received: from shire.ontko.com (shire.ontko.com [199.164.165.1]) by svr1.postgresql.org (Postfix) with ESMTP id 91014537E9 for ; Sun, 8 May 2005 18:48:40 -0300 (ADT) Received: from [127.0.0.1] (waymoot.ontko.com [199.164.165.31]) by shire.ontko.com (8.12.3/8.12.3/Debian-7.1) with ESMTP id j48LmbXw013313; Sun, 8 May 2005 16:48:39 -0500 Message-ID: <427E892A.1000107@ontko.com> Date: Sun, 08 May 2005 16:48:26 -0500 From: Nick Fankhauser Reply-To: nickf@doxpop.com Organization: Ray Ontko & Co. User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Alvaro Herrera Cc: nickf@doxpop.com, pgsql-sql@postgresql.org Subject: Re: interesting SQL puzzle - concatenating column with itself. References: <427E60BC.8060701@ontko.com> <20050508212905.GA14422@dcc.uchile.cl> In-Reply-To: <20050508212905.GA14422@dcc.uchile.cl> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200505/86 X-Sequence-Number: 21399 Excellent! Thanks for providing both the idea and an example. I didn't get the idea right away, but the example made it clear. I'll try that on my table and report back on how it works out. Regards, -Nick ------------------------------------------------------------------ Nick Fankhauser nickf@ontko.com Phone 765.935.4283 Fax 765.962.9788 Ray Ontko & Co. - Software Consulting Services http://www.ontko.com Alvaro Herrera wrote: > > You can create a custom aggregate to do concatenation and group by > memo_id. > > create or replace function text_cat(text, text) returns text called on null input > language sql immutable as 'select case when $1 is null then $2 when $2 > is null then $1 else $1 || '' '' || $2 end'; > > create aggregate textcat (basetype = text, sfunc = text_cat, stype = text); > > create table memos (memo_id int, sequence int, memo_text text); > insert into memos values (666, 3, 'jumped over'); > insert into memos values (666, 1, 'The quick'); > insert into memos values (666, 4, 'the lazy brown dog'); > insert into memos values (666, 2, 'red fox'); > > select memo_id, textcat(memo_text) from (select * from memos order by > memo_id, sequence) as foo group by memo_id; > > > The order is not really guaranteed, though if this is a one-shot thing, > you may get away with turning off hashed aggregates. >