Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 2A2471DEB3D6 for ; Mon, 23 Jul 2012 01:46:01 -0300 (ADT) Received: from nm3-vm1.bullet.mail.ne1.yahoo.com ([98.138.91.53]) by makus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1StAWu-00036e-H0 for pgsql-sql@postgresql.org; Mon, 23 Jul 2012 04:46:01 +0000 Received: from [98.138.90.55] by nm3.bullet.mail.ne1.yahoo.com with NNFMP; 23 Jul 2012 04:45:47 -0000 Received: from [98.138.89.170] by tm8.bullet.mail.ne1.yahoo.com with NNFMP; 23 Jul 2012 04:45:47 -0000 Received: from [127.0.0.1] by omp1026.mail.ne1.yahoo.com with NNFMP; 23 Jul 2012 04:45:47 -0000 X-Yahoo-Newman-Id: 379183.81868.bm@omp1026.mail.ne1.yahoo.com Received: (qmail 30531 invoked from network); 23 Jul 2012 04:45:47 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=QKcpX6nbp/wNQOHb+b7jpUOMPAeddJk6AY+a0pg/hNdV8o7sx8PPB9YOk2KIHm5VSx3iTyHZmolA+/pKwwhgmZmLtSZVjfrx8sP99IT7l4FmTiq+b7REsdROoMrjZzCC35WA5H4rRnJhImB1GPMFrkwfxVkpMZjZ1j2NKH1OGGk= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1343018747; bh=eDEwys54D9JxcEPRbHNZ9Kt2V1YKxE+9zOaw/ArPKqI=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=VPJmR4a9GrWXTrCG5kPnfvs27IkgyQ1GiDqTnCnVhjpeU7zn7ao3fuZP3ff6rkgCv4iLTfLhtdsWsyQJLqbLeYOL52m45j5lo6Kt7j9GF1Fx+Mw6yo6LIfSEHBRn54T79efhPFzBJCbWfw+9NUzs38WobqEH92A3mxTLDV48kIM= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: U0YVfk0VM1nbYeXWfBc0rGtZ0QZzm.gMtvHg12QHidkaWep JXKy2nKriB5I6GxQJ8wGAX0JgN22I15MCmisbuotBrLMGOmcmy4OiLpCVnnK qCcA.EwNNQ7anzmYX6I681YswF9nlxwWaWmh_OPe8i4.E9ySFd7BIgl1UiKJ X46pYHyaKSUdEP4lqCZudphkVxjaEFhEuckJC3GMNBDcDvmItz2RRGR9mmK6 V9R4Tue8741awyxXjatcriFOrEsTs3InkoOsYFhTz4Tt_T1e6P7qqlTmd7Tr L69vuXTWotl2opwj9LFZC2rWIWe9SF9S2fMpv3K1ZA0rzxNZ1sWrH8wlOg5r S1lVg1TyGXUVsBLAsZhm2.3WFq_J4RTqgs49Z7H6iSiY.QJ.RJYmsavyTlRQ nS45UhQ3r.bNCQbXv X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from [192.12.17.104] (polobo@24.93.23.188 with xymcookie) by smtp115-mob.biz.mail.ne1.yahoo.com with SMTP; 22 Jul 2012 21:45:46 -0700 PDT References: <1343012672084-5717583.post@n5.nabble.com> In-Reply-To: <1343012672084-5717583.post@n5.nabble.com> Mime-Version: 1.0 (1.0) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Message-Id: <68C10F74-B9BC-4D2A-9B5A-A2E935FEFA78@yahoo.com> Cc: "pgsql-sql@postgresql.org" X-Mailer: iPad Mail (9B206) From: David Johnston Subject: Re: query two tables using same lookup table Date: Mon, 23 Jul 2012 00:45:47 -0400 To: ssylla X-Pg-Spam-Score: 0.0 (/) X-Archive-Number: 201207/29 X-Sequence-Number: 36766 On Jul 22, 2012, at 23:04, ssylla wrote: > Dear list,=20 >=20 > assuming I have two tables as follows=20 >=20 > t1:=20 > id_project|id_auth=20 > 1|1=20 > 2|2=20 >=20 > t2:=20 > id_project|id_auth=20 > 1|2=20 > 2|1=20 >=20 >=20 > and a lookup-table:=20 >=20 > t3=20 > id_auth|name_auth=20 > 1|name1=20 > 2|name2=20 >=20 > Now I want to query t1 an t2 using the 'name_auth' column of lookup-table > t3, so that I get the following output:=20 > id_project|name_auth_t1|name_auth_t2=20 > 1|name1|name2=20 > 2|name2|name1=20 >=20 > Any ideas?=20 >=20 > Thanks-=20 > Stefan >=20 >=20 Not tested, may need minor syntax cleanup but the theory is sound. With pj as ( Select id_project, id_name1, id_name2 =46rom (select id_project, id_auth as id_auth1 from t1) s1 Natural Full outer join (select id_project, id_auth as id_auth2 from t2) s2 ) Select pj.id_project, n1.name_auth, n2.name_auth =46rom pj Left join t3 as n1 on (id_auth1 =3D id_auth) Left join t3 as n2 on (id_auth2 =3D id_auth) ; Full join the two project tables and give aliases to the duplicate id_auth f= ield. Then left join against t3 twice (once for eachid_auth) using yet a an= other set of aliases to distinguish them. David J.