Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 42F6F2E3A9B for ; Fri, 21 Dec 2007 09:49:08 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 87002-06 for ; Fri, 21 Dec 2007 09:48:47 -0400 (AST) Received: from mail.attiksystem.ch (f29.attiksystem.ch [212.147.59.29]) by postgresql.org (Postfix) with ESMTP id 7CA172E3AA0 for ; Fri, 21 Dec 2007 09:48:58 -0400 (AST) Received: from poweredge.attiksystem.ch (poweredge.attiksystem.ch [10.0.0.29]) by mail.attiksystem.ch (8.12.11/8.12.11) with ESMTP id lBLDmt3l025050 for ; Fri, 21 Dec 2007 14:48:55 +0100 (CET) (envelope-from philippe.lang@attiksystem.ch) Content-Class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: Re: Concatenation through SQL Date: Fri, 21 Dec 2007 14:48:55 +0100 X-MimeOLE: Produced By Microsoft Exchange V6.0.6619.12 Message-ID: <6C0CF58A187DA5479245E0830AF84F4218CFBE@poweredge.attiksystem.ch> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [SQL] Concatenation through SQL thread-index: AchDwIiVIEL+nuqgQDSayMSVSEJdWQAFnQsg References: <6C0CF58A187DA5479245E0830AF84F42337F97@poweredge.attiksystem.ch> <20071221105813.GA18247@KanotixBox> From: "Philippe Lang" To: X-Scanned-By: MIMEDefang 2.49 on 10.0.0.111 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/113 X-Sequence-Number: 29805 pgsql-sql-owner@postgresql.org wrote: > Philippe Lang schrieb: >=20 > 2 ways: >=20 > * You can use something like this: >=20 > test=3D*# select * from a; > id | val > ----+----- > 1 | foo > 2 | bar > (2 rows) >=20 > test=3D*# select array_to_string(array(select val from a), ', ');=20 > array_to_string ----------------- > foo, bar > (1 row) >=20 >=20 > * comma-aggregate, see > http://www.zigo.dhs.org/postgresql/#comma_aggregate Hi, Thanks to all who responded, in particular Andreas. I'm always amazed by the quality of both the Postgresql database and the support in its newsgroups. The "comma-aggregate" worked just fine for me. Here is the final example, for those willing to test it. This will be more than useful! --------------------------------------- CREATE TABLE appointments ( id integer, name varchar(32), CONSTRAINT appointments_pkey PRIMARY KEY (id) ); CREATE TABLE employees ( id integer, appointment_id integer, name varchar(32), CONSTRAINT employees_pkey PRIMARY KEY (id), CONSTRAINT appointments_employees_fkey FOREIGN KEY (appointment_id) REFERENCES appointments (id) ); INSERT INTO appointments VALUES (1, 'app1'); INSERT INTO employees VALUES (1, 1, 'emp1'); INSERT INTO employees VALUES (2, 1, 'emp2'); INSERT INTO employees VALUES (3, 1, 'emp3'); INSERT INTO appointments VALUES (2, 'app2'); INSERT INTO employees VALUES (4, 2, 'emp1'); INSERT INTO employees VALUES (5, 2, 'emp4'); CREATE AGGREGATE array_accum (anyelement)=20 (=20 sfunc =3D array_append,=20 stype =3D anyarray,=20 initcond =3D '{}'=20 );=20 SELECT=20 appointments.name AS appointment, (SELECT COUNT(*) FROM employees AS e where e.appointment_id =3D appointments.id) AS num_employees, (SELECT array_to_string(array_accum(name),', ') FROM employees AS e WHERE e.appointment_id =3D appointments.id) AS employees FROM appointments --------------------------------------- Result: --------------------------------------------------- appointment count_employees employees --------------------------------------------------- app1 3 emp1, emp2, emp3 app2 2 emp1, emp4, --------------------------------------------------- Merry christmas to all. Philippe