Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 9E6E82E2D43 for ; Fri, 21 Dec 2007 12:01:37 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 29087-03 for ; Fri, 21 Dec 2007 12:01:31 -0400 (AST) Received: from mail.attiksystem.ch (f29.attiksystem.ch [212.147.59.29]) by postgresql.org (Postfix) with ESMTP id D7A092E2D45 for ; Fri, 21 Dec 2007 12:01:30 -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 lBLG1S8D025707 for ; Fri, 21 Dec 2007 17:01:28 +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 17:01:28 +0100 X-MimeOLE: Produced By Microsoft Exchange V6.0.6619.12 Message-ID: <6C0CF58A187DA5479245E0830AF84F4218CFC1@poweredge.attiksystem.ch> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [SQL] Concatenation through SQL thread-index: AchD3UMhc5glFieTR++MEVKvLR8a2gADGvig References: <6C0CF58A187DA5479245E0830AF84F42337F97@poweredge.attiksystem.ch> <20071221105813.GA18247@KanotixBox> <6C0CF58A187DA5479245E0830AF84F4218CFBE@poweredge.attiksystem.ch> <162867790712210624y1e3454ffw8d4bffff69819335@mail.gmail.com> 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/115 X-Sequence-Number: 29807 Pavel Stehule wrote: =20 >> SELECT >> 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 --------------------------------------- >>=20 >=20 > hello, >=20 > one note: you can merge your two subselect into one. It's unimportant > on 100 rows, but it's can be important on thousands rows.=20 >=20 > nice a day > Pavel Hi Pavel, Since subselects must return one single column, is that what you are talking about? I have just modified the code you have posted before. --------------------------- SELECT=20 name AS appointment,=20 num_employees,=20 employees=20 FROM=20 ( SELECT=20 appointment_id,=20 COUNT(*) AS num_employees,=20 (SELECT array_to_string(array_accum(name),', ') FROM employees WHERE e.appointment_id =3D appointment_id) AS employees FROM employees e=20 GROUP BY appointment_id )s JOIN appointments a on a.id =3D s.appointment_id; --------------------------- Philippe