Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id BC0876327AA for ; Wed, 24 Feb 2010 10:39:50 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 44089-01 for ; Wed, 24 Feb 2010 14:39:31 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smadev.internal.net (host3.dynacom.ondsl.gr [62.103.35.211]) by mail.postgresql.org (Postfix) with ESMTP id 1AFC96323E9 for ; Wed, 24 Feb 2010 10:39:29 -0400 (AST) Received: from smadev.internal.net (localhost [127.0.0.1]) by smadev.internal.net (8.14.2/8.14.2) with ESMTP id o1OEd1Ne017550 for ; Wed, 24 Feb 2010 16:39:01 +0200 (EET) (envelope-from achill@matrix.gatewaynet.com) Received: from localhost (localhost [[UNIX: localhost]]) by smadev.internal.net (8.14.2/8.14.2/Submit) id o1OEd0TX017549 for pgsql-sql@postgresql.org; Wed, 24 Feb 2010 16:39:00 +0200 (EET) (envelope-from achill@matrix.gatewaynet.com) From: Achilleas Mantzios Organization: Dynacom Tankers Mgmt To: pgsql-sql@postgresql.org Subject: Re: join with an array Date: Wed, 24 Feb 2010 16:39:00 +0200 User-Agent: KMail/1.9.7 References: <20100224124023.GA16500@apartia.fr> <20100224130909.GL22249@a-kretschmer.de> <20100224133447.GA18435@apartia.fr> In-Reply-To: <20100224133447.GA18435@apartia.fr> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <201002241639.00331.achill@matrix.gatewaynet.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.67 tagged_above=-10 required=5 tests=AWL=-0.560, BAYES_05=-1.11 X-Spam-Level: X-Archive-Number: 201002/93 X-Sequence-Number: 33692 =CE=A3=CF=84=CE=B9=CF=82 Wednesday 24 February 2010 15:34:48 =CE=BF/=CE=B7 = Louis-David Mitterrand =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5: > Here is a test case I built. I want to list all cruises by cruise_type > but after merging cruise_type that have the same cruise_type_name: >=20 > drop table cruise; > drop table cruise_type; >=20 > create table cruise_type ( > id_cruise_type serial primary key, > cruise_type_name text > ); >=20 > create table cruise ( > id_cruise serial, > id_cruise_type integer references cruise_type, > cruise_date timestamp default now() > ); >=20 > insert into cruise_type (cruise_type_name) values=20 > ('5 day eastern carribean cruise'), > ('5 day western carribean cruise'), > ('5 day eastern carribean cruise'), > ('5 day western carribean cruise') > ; >=20 > insert into cruise (id_cruise_type) values=20 > (1), > (2), > (3), > (4), > (1), > (2), > (3), > (4) > ; >=20 > select array_agg(ct.id_cruise_type),ct.cruise_type_name from cruise_type = ct join cruise c on (c.id_cruise =3D any(array_agg)) group by cruise_type_n= ame; >=20 You dont specify (in english) what you exactly want to achive, but here is = my shot: 1st, get the cruises by cruise type: select ct.id_cruise_type,array_agg(c.id_cruise) as "List of Cruises" from c= ruise_type ct, cruise c WHERE c.id_cruise_type=3Dct.id_cruise_type GROUP BY= ct.id_cruise_type ORDER BY ct.id_cruise_type; id_cruise_type | List of Cruises =2D---------------+----------------- 1 | {1,5} 2 | {2,6} 3 | {3,7} 4 | {4,8} (4 rows) test=3D# =20 Then you may pretify this to include the name of each cruise type as well: select ct.id_cruise_type,ct.cruise_type_name,array_agg(c.id_cruise) as "Lis= t of Cruises" from cruise_type ct, cruise c WHERE c.id_cruise_type=3Dct.id_= cruise_type GROUP BY ct.id_cruise_type,ct.cruise_type_name ORDER BY ct.id_c= ruise_type; id_cruise_type | cruise_type_name | List of Cruises =2D---------------+--------------------------------+----------------- 1 | 5 day eastern carribean cruise | {1,5} 2 | 5 day western carribean cruise | {2,6} 3 | 5 day eastern carribean cruise | {3,7} 4 | 5 day western carribean cruise | {4,8} (4 rows) EXERCISE: Why cant we exclude ct.id_cruise_type from the select clause and group by o= f the above query? =2D-=20 Achilleas Mantzios