Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wv5aZ-0000Aq-NM for pgsql-sql@arkaria.postgresql.org; Thu, 12 Jun 2014 14:02:47 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Wv5aY-0002lR-R5 for pgsql-sql@arkaria.postgresql.org; Thu, 12 Jun 2014 14:02:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Wv5aX-0002j6-LV for pgsql-sql@postgresql.org; Thu, 12 Jun 2014 14:02:45 +0000 Received: from sam.nabble.com ([216.139.236.26]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1Wv5aT-0006VC-BC for pgsql-sql@postgresql.org; Thu, 12 Jun 2014 14:02:44 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1Wv5aR-0005a0-4h for pgsql-sql@postgresql.org; Thu, 12 Jun 2014 07:02:39 -0700 Date: Thu, 12 Jun 2014 07:02:39 -0700 (PDT) From: David G Johnston To: pgsql-sql@postgresql.org Message-ID: <1402581759135-5807003.post@n5.nabble.com> In-Reply-To: References: Subject: Re: Problem with duplicate rows when FULL OUTER JOIN'ing 3 derived tables MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 0.8 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Note - numbers do not relate to your questions. This pretty much answers 1 and 3. 1. I'd ensure that project name/id on a table can never be null by creating a dummy project that means "none assigned". 2. I would avoid full join. To do so I'd cross join a distinct list of companies with a distinct list of projects. 3. Against the join in 2 you then left join three times, once for each product table. 4. In the final result any product not having a correspond company/project would have its value coalesced to zero. The end result is a table without any NULL and, in the case of your explanatory data, 9 rows - two of which would be all zeros: (c3,p1) and (c3,p,2) If you cannot change the raw data I'd use CTE/WITH to normalize the data according to 1 and the use these CTEs in the rest of the query. I would also do 2 is a CTE then 3 would be normal subqueries or relation references as necessary. David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/Problem-with-duplicate-rows-when-FULL-OUTER-JOIN-ing-3-derived-tables-tp5806970p5807003.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql