Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 4F9D2476807 for ; Tue, 10 Dec 2002 13:34:04 -0500 (EST) Received: from joeconway.com (unknown [63.210.180.150]) by postgresql.org (Postfix) with ESMTP id 3703E476787 for ; Tue, 10 Dec 2002 13:33:20 -0500 (EST) Received: from [206.19.64.3] (account jconway HELO joeconway.com) by joeconway.com (CommuniGate Pro SMTP 3.5.9) with ESMTP-TLS id 1475176; Tue, 10 Dec 2002 11:12:28 -0800 Message-ID: <3DF6330D.1090900@joeconway.com> Date: Tue, 10 Dec 2002 10:31:41 -0800 From: Joe Conway User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.2) Gecko/20021126 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Elizabeth O'Neill's Office Mail Cc: pgsql-sql@postgresql.org Subject: Re: SQL syntax for concating values in different rows together References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200212/115 X-Sequence-Number: 10703 Elizabeth O'Neill's Office Mail wrote: > I have two tables in my database a complaint table and a resolution table. > One complaint may have several resolutions. I am trying to build a report > that will give me the complaint details and all the resolution descriptions > for a complaint in one text area/row (concated together). > > At the moment it is repeating the complaint details for each resolution. As someone else mentioned I think, you can use a plpgsql function. Here is a contrived example: create table complaint(cid int, descr text); insert into complaint values(1,'my #1 complaint'); insert into complaint values(2,'my #2 complaint'); create table resolution(rid int, cid int, res text); insert into resolution values (1,1,'fixed it'); insert into resolution values (2,1,'really fixed it!'); insert into resolution values (3,2,'pbkbac again'); create or replace function my_concat(int) returns text as' declare ret text; comp text; rec record; cntr int = 0; begin select into comp descr from complaint where cid = $1; ret := ''Comp = '' || comp; for rec in select res from resolution where cid = $1 loop cntr := cntr + 1; ret := ret || '': Res# '' || cntr::text || '' = '' || rec.res; end loop; return ret; end; ' language 'plpgsql'; regression=# select my_concat(cid) from complaint; my_concat ---------------------------------------------------------------------- Comp = my #1 complaint: Res# 1 = fixed it: Res# 2 = really fixed it! Comp = my #2 complaint: Res# 1 = pbkbac again (2 rows) In the past I think I remember someone trying to solve this kind of problem with a custom aggregate, but the plpgsql approach is probably simpler. HTH, Joe