Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TI0vN-0004H2-CA for pgsql-sql@postgresql.org; Sat, 29 Sep 2012 17:33:57 +0000 Received: from nm1-vm0.bullet.mail.ac4.yahoo.com ([98.139.53.202]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TI0vL-0000wW-19 for pgsql-sql@postgresql.org; Sat, 29 Sep 2012 17:33:57 +0000 Received: from [98.139.52.188] by nm1.bullet.mail.ac4.yahoo.com with NNFMP; 29 Sep 2012 17:33:53 -0000 Received: from [98.139.52.171] by tm1.bullet.mail.ac4.yahoo.com with NNFMP; 29 Sep 2012 17:33:53 -0000 Received: from [127.0.0.1] by omp1054.mail.ac4.yahoo.com with NNFMP; 29 Sep 2012 17:33:53 -0000 X-Yahoo-Newman-Id: 586662.27492.bm@omp1054.mail.ac4.yahoo.com Received: (qmail 81088 invoked from network); 29 Sep 2012 17:33:53 -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=NdedJwHcpWmo7RMyFlPpFkSaRICVTM9aNU81E1XSmS69Y5yWuCSCEh9RCnBggoaVySyJpunpxvgaXOXLjVgHEDl9OQmrcpbZDmutO9c1y9bbGkk36wUGWc/YxkIZiAmItQiozOelBza44ehG2vTUDy8Z5F1UHwjCYu4CUyrSvEk= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1348940033; bh=tYGMpR+VQVzLTJ9N65g3N1fyD2pRMIUWtZl1lVM9qD4=; 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=JsKg1RRFCju+iE+LS6fXQUnWje1DlDIrMyJy7LgjPNjwDQi/1JLZyIgwSmW7KiCxhdFu2Ba+2hFoX0emA7/kBxuiwazI00kM/XtbqaYoYexaa0aNE2wiDZ1jNrA9lFAH+1s40kj+3DhJTtqcQtPP9fuWWiFoe1WfjJYKA3IZKgk= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: tHJPJ6cVM1koApZib_sPlzjav8YYO3tivTrLlx2X4_gdfN_ .AewPj6EtR1JA0q9b_1SrIOucfTiOdsjlz.288unG5tmSFRH0dwoob8WlKzh _sbxJ.Xpt0Dd0TWKXxFyxY0mW6l8qDYFY_5sQOT.cHlzCyYzkkzmtfDAjXRu EoxY7mJGm_pNnwzqJCNCn40sMPBvuTiczC8vzVrjwSPbKj95.WLAoVm6FRVF _iKdapUe6Hx2vwF7S2u.FTD7_OVqm5ZEt5fBRWvg7SawAK.sTR4bKHWLbL.q BnHLbO8F5ZPH0Oqk7IbHuhaX6roRAWsVeFj86mXotkagAELKs7LfCHNIM9Wo VAGwhEGfGNRChARwTBkRxQnbnv5dwdrtH4y0yJC2niABhECJdEkJ6FubGeeT wskal7VhgjSAaHmYCsGwtik5ggaCrEzSMrlp3Ohs6Ot8U2k12tUyYx07e8JN T1rP0 X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from [192.12.17.101] (polobo@24.93.23.188 with xymcookie) by smtp119-mob.biz.mail.ac4.yahoo.com with SMTP; 29 Sep 2012 10:33:53 -0700 PDT References: <50671B8F.4090504@gmx.net> In-Reply-To: <50671B8F.4090504@gmx.net> Mime-Version: 1.0 (1.0) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Message-Id: Cc: "pgsql-sql@postgresql.org" X-Mailer: iPad Mail (9B206) From: David Johnston Subject: Re: Need help with a special JOIN Date: Sat, 29 Sep 2012 13:33:56 -0400 To: Andreas X-Pg-Spam-Score: -2.8 (--) X-Archive-Number: 201209/66 X-Sequence-Number: 36868 On Sep 29, 2012, at 12:02, Andreas wrote: > Hi, >=20 > asume I've got 2 tables >=20 > objects ( id int, name text ) > attributes ( object_id int, value int ) >=20 > attributes has a default entry with object_id =3D 0 and some other where= another value should be used. >=20 > e.g. > objects > ( 1, 'A' ), > ( 2, 'B' ), > ( 3, 'C' ) >=20 > attributes > ( 0, 42 ), > ( 2, 99 ) >=20 > The result of the join should look like this: >=20 > object_id, name, value > 1, 'A', 42 > 2, 'B', 99 > 3, 'C', 42 >=20 >=20 > I could figure something out with 2 JOINs, UNION and some DISTINCT ON but t= his would make my real query rather chunky. :( >=20 > Is there an elegant way to get this? >=20 General form (idea only, syntax not tested) Select objectid, name, coalesce(actuals.value, defaults.value) =46rom objects cross join (select ... =46rom attributes ...) as defaults Left join attributes as actuals on ... Build up a master relation with all defaults then left join that against the= attributes taking the matches where present otherwise taking the default. David J.