agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: David G Johnston <david.g.johnston@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: Problem with duplicate rows when FULL OUTER JOIN'ing 3 derived tables
Date: Thu, 12 Jun 2014 07:02:39 -0700 (PDT)
Message-ID: <1402581759135-5807003.post@n5.nabble.com> (raw)
In-Reply-To: <VisenaEmail.1ab.5c43ef9d9d1223b4.1468f6a10b4@tc7-on>
References: <VisenaEmail.1ab.5c43ef9d9d1223b4.1468f6a10b4@tc7-on>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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-deriv...
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
view thread (10+ messages) latest in thread
Message-ID: <1402581759135-5807003.post@n5.nabble.com>
Permalink: ../1402581759135-5807003.post@n5.nabble.com/
Also on: postgresql.org/message-id/1402581759135-5807003.post@n5.nabble.com
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: david.g.johnston@gmail.com
Subject: Re: Problem with duplicate rows when FULL OUTER JOIN'ing 3 derived tables
In-Reply-To: <1402581759135-5807003.post@n5.nabble.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox