Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ixamj-0002Gn-3f for pgsql-general@arkaria.postgresql.org; Fri, 31 Jan 2020 18:12:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ixamg-0000Rw-CG for pgsql-general@arkaria.postgresql.org; Fri, 31 Jan 2020 18:12:50 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ixamg-0000Rg-2f for pgsql-general@lists.postgresql.org; Fri, 31 Jan 2020 18:12:50 +0000 Received: from mta2.recol.net ([64.207.103.7]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ixamd-0006Hz-47 for pgsql-general@lists.postgresql.org; Fri, 31 Jan 2020 18:12:49 +0000 Received: from wrkhors.com (rrcs-208-105-145-68.nys.biz.rr.com [208.105.145.68]) by mta2.recol.net (Postfix) with ESMTPSA id DA25B398117; Fri, 31 Jan 2020 13:12:44 -0500 (EST) Date: Fri, 31 Jan 2020 12:06:05 -0600 From: Steven Lembark To: pgsql-general@lists.postgresql.org Cc: lembark@wrkhors.com Subject: Re: combination join against multiple tables Message-ID: <20200131120605.4df7157e.lembark@wrkhors.com> In-Reply-To: References: Organization: Workhorse Computing X-Mailer: Claws Mail 3.17.3 (GTK+ 2.24.32; x86_64-pc-linux-gnu) X-Workhorse: Quite MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, 31 Jan 2020 14:01:17 +0000 Geoff Winkless wrote: > a | c | c > ----+-----+----- > 1 | 111 | 211 > 1 | 112 | > 2 | 121 | > 2 | | 222 > 3 | | > 4 | 141 | > 5 | | 253 > 6 | | > 7 | | > 8 | | > 9 | | > 10 | | The c's look just like a full outer join of t1 & t2 on a & b. Giving them saner names to avoid duplicate output col's, let's call them "c1" & "c2". At that point a left outer join on a gives you all of the base.a values with any t{1,2} rows that have a matching a: No idea what your data really looks like but if t1 or t2 has more than three col's distinct can save some annoying cross-products: select distinct base.a , z.c1 , z.c2 from base left join ( select t1.a , t1.c "c1" , t2.c "c2" from t1 full outer join t2 on t1.a = t2.a and t1.b = t2.b ) z on base.a = z.a ; -- Steven Lembark 3646 Flora Place Workhorse Computing St. Louis, MO 63110 lembark@wrkhors.com +1 888 359 3508