sql - Left Outer join not returning expected resultSet -


i have these 2 tables

              #table1# ##companyname##    ##primarykey## dell                     1 dell california          2 dell san francisco       3 dell los angeles         4 ibm                      5 google                   6                                       #table2# ##parentcomp##   ##parentprimarykey##       ##childcomp##    ##childprimarykey##     dell                     1             dell california            2 dell california              2             dell san francisco         3 dell california              2             dell los angeles           4 

now expected table shown below; relationship(hierarchy) column has 3 possible values(parent/child/independent) shown:

                               #table3# ##companyname##             ##relationship##                     ##parentcompany## dell                             parent                                --- dell california                  child                                 dell dell san francisco               child                             dell california dell los angeles                 child                             dell california ibm                           independent                              --- google                        independent                              --- 

i have tried following 2 join options

select
...
table1
left outer join
table2 on table1.primarykey = table2.parentprimarykey
left outer join
table2 talias2
table2.parentprimarykey = talias2.childprimarykey

returns

 ##companyname##               ##relationship##                     ##parentcompany##     dell                             parent                                ---     dell california                  child                                 dell     dell san francisco            independent                              ---     dell los angeles              independent                              ---     ibm                           independent                              ---     google                        independent                              --- 

or

select
...
table1
left outer join
table2 on table1.primarykey = table2.childprimarykey
left outer join
table1 talias1
table2.parentprimarykey = talias1.primarykey

will return this:

 ##companyname##               ##relationship##                     ##parentcompany##     dell                           independent                             ---     dell california                  child                                 dell     dell san francisco               child                              dell california     dell los angeles                 child                              dell california     ibm                            independent                             ---     google                         independent                             --- 

when join talias2.childprimarykey need join table1.primarykey column.

select ..   table1         left outer join table2                      on table1.primarykey = table2.parentprimarykey         left outer join table2 talias2                      on table1.primarykey = talias2.childprimarykey --here 

when join talias2.childprimarykey table2.parentprimarykey matched records table2 , table1(table1.primarykey = table2.parentprimarykey) joined talias2.childprimarykey


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -