X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 06B86475EA9 for ; Mon, 24 Feb 2003 15:11:05 -0500 (EST) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with SMTP id 58241-05 for ; Mon, 24 Feb 2003 15:10:54 -0500 (EST) Received: from news.hub.org (news.hub.org [64.49.215.80]) by postgresql.org (Postfix) with ESMTP id 0B232474E42 for ; Mon, 24 Feb 2003 14:34:15 -0500 (EST) Received: by news.hub.org (Postfix, from userid 8) id E199043A69E; Mon, 24 Feb 2003 14:34:14 -0500 (EST) X-Abuse-Report: abuse@teranews.com Message-ID: <988d394c6d80ecd1396eac17ff787470@news.teranews.com> Date: Mon, 24 Feb 2003 19:12:03 GMT From: Jeff Boes Subject: Re: Denormalizing during select X-Newsgroups: comp.databases.postgresql.sql Organization: Nexcerpt, Inc. References: User-Agent: Pan/0.11.4 (Unix) Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 X-Comment-To: "no.spam" Lines: 36 To: pgsql-sql@postgresql.org Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: by amavisd-new X-Archive-Number: 200302/535 X-Sequence-Number: 11933 On Mon, 24 Feb 2003 12:18:24 -0500, no.spam wrote: > I have two tables (A and B) in a one-to-many relationship. When > retrieving data from one table (A), I need to denormalize the tables > during a select so that values of a column in the many table (B) appear > as a list in one field of the output. Well, the straightforward way to do this would be with a function: create or replace function my_func(text) returns text as ' declare name_one alias for $1; accum text; result record; begin accum :=3D \'\'; for result in select property from many_table where name =3D name_one loop if accum !=3D \'\' then accum :=3D accum || \',\'; end if; accum :=3D accum || result.property; end loop; return accum; end; ' language 'plpgsql'; Note that the quotes internal to the function must be escaped. --=20 Jeff Boes vox 269.226.9550 ext 24 Database Engineer fax 269.349.9076 Nexcerpt, Inc. http://www.nexcerpt.com ...Nexcerpt... Extend your Expertise