Link to home
Start Free TrialLog in
Avatar of Dale Fye
Dale FyeFlag for United States of America

asked on

Comparing data in three tables

Looking to compare data from three different data sources, I have far more columns to include in the comparison, but for simplicity

Lets just say I have:

Table_A                  Table_B                  Table_C
ID   Somevalue           ID    Somevalue          ID     Somevalue
1          3              1          3             2            5 
2          4              3          5             3            6

Open in new window

I would like the query results as:

ID   Value_A    Value_B    Value_C
1          3          3       NULL
2          4       NULL          5
3       NULL          5          6

Open in new window


This obviously involves at least one Full Outer Join

SELECT Coalesce(A.ID, B.ID) as ID, A.SomeValue as Value_A, B.SomeValue as Value_B
FROM SELECT ID, SomeValue FROM Table_A as A
FULL OUTER JOIN
SELECT ID, SomeValue from Table_B as B
ON A.ID = B.ID

But I'm confused on how to accomplish this for Table_C.  Do I have to wrap the above as a subquery or CTE and then join that to Table_C, or can I simply add another line for Table_C (and if the latter) do I join on A.ID = C.ID or B.ID = C.ID or some other join condition?

Or would some sort of union query as a subquery and a Pivot query work better
ASKER CERTIFIED SOLUTION
Avatar of OMC2000
OMC2000
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dale Fye

ASKER

That might work, I'll give it a try.
That worked perfectly.