Re: Problem with duplicate rows when FULL OUTER JOIN'ing 3 derived tables - Mailing list pgsql-sql

From David G Johnston
Subject Re: Problem with duplicate rows when FULL OUTER JOIN'ing 3 derived tables
Date
Msg-id 1402581759135-5807003.post@n5.nabble.com
Whole thread Raw
In response to Problem with duplicate rows when FULL OUTER JOIN'ing 3 derived tables  (Andreas Joseph Krogh <andreas@visena.com>)
Responses Re: Re: Problem with duplicate rows when FULL OUTER JOIN'ing 3 derived tables  (Andreas Joseph Krogh <andreas@visena.com>)
List pgsql-sql
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.



pgsql-sql by date:

Previous
From: Andreas Joseph Krogh
Date:
Subject: Problem with duplicate rows when FULL OUTER JOIN'ing 3 derived tables
Next
From: Andreas Joseph Krogh
Date:
Subject: Re: Re: Problem with duplicate rows when FULL OUTER JOIN'ing 3 derived tables