I'm joining a subquery to outer query and need to see the results of everything, even ones that the joining is not matching.

I tried to create a Temp table for the outer query and a temp table for one of the inner query then tried to join on that. I tried to do an insert to outer query, but it gave me a ton of rows. I'm thinking of a Merging statement with a target table with matching and nonmatching.

Select 
col a  --- Can have Nulls or blanks in Table A
col b
col c
col d
col e
from table a
left join

( select
col a  This will never have blanks or nulls
col b
col c
col d
col e
from table)b

on a.cola =  b.cola  -- even though there might be nulls in table A show 
                        them. so A.ColA <> B.ColA and carry them to the next subquery
and a.colb = b.colb
and a.colc = b.colc
Left join

Whatever is NUll for ColA will go to the next subquery
(select
col a
col b
col c
col d
col e
from table)C

on a.colb =  b.colb
and a.colc = b.colc

When there is a match it will show up, but I need to see the ones that have no match so I can have it go to the next subquery to see if there's a match.

3 Answers

0
APH On

Without more details on your question and dbms, I think this will help, but am not sure:

Select a.*, ISNULL(b.col, c.col) --Will show you the column from b if it matched, or the column from c if there was no match on b
from Table a
left join Table b
on a.col = b.col... --More restrictive criteria here
left join Table c
on a.col = c.col...--Less restrictive criteria here
0
Ravi Sharma On

select distinct t1.Id,t1.col1,t1.col2,t2.col1 from table1 t1 left join table2 t2 on t1.Id != t2.Id

0
Gordon Linoff On

You seem to want cascading logic in the join. Your question is a bit vague on the specifics, but it looks something like this:

Select coalesce(a.cola, c.cola) as cola,
       a.colb, a.colc,
       coalesce(b.cold, c.cold) as cold
       coalesce(b.cole, c.cole) as cole
from a left join
     b
     on a.cola = b.cola and
        a.colb = b.colb and
        a.colc = b.colc left join
     c
     on a.colA is null and  -- or is this b.colA
        a.colb = b.colb and
        a.colc = b.colc