Results 1 to 6 of 6

Thread: Join or Union?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    350

    Join or Union?

    I have two tables:
    Table 1:
    Code,No1
    A, 1
    C, 2
    E, 3

    Table 2:
    Code, No2
    A, 2
    B, 4
    D, 6

    I want to join these two tables to give;
    Code,No1,No2
    A, 1, 2
    B, 0, 4
    C, 2, 0
    D, 0, 6
    E, 3, 0

    I have been trying varius JOINS & UNION but I cannot find the solution - any suggestions.
    Thanks.

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Join or Union?

    Looks like a Full Outer to me:-

    Select isnull(table1.No1), isnull(table2.No2)
    From Table1
    Full Outer Join Table2
    on Table1.Code = Table2.Code




    edit> If you Joined a Union would it have to be a Left Join?
    Last edited by FunkyDexter; Sep 18th, 2012 at 09:37 AM. Reason: Never could resist a quip
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    350

    Re: Join or Union?

    That's neary what I want, it's not giving the code
    No1,No2
    1, 2
    0, 4
    2, 0
    0, 6
    3, 0

    I tried adding it to the SELECT satement but it leaves blanks.

    Select table1.code, isnull(table1.No1), isnull(table2.No2)
    From Table1
    Full Outer Join Table2
    on Table1.Code = Table2.Code

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263

    Re: Join or Union?

    Worked with Access

    Code:
    SELECT table4.Code, table4.No1, iif(isnull(table2.No2),0,table2.No2) As No2 From
    (SELECT DISTINCT(table3.Code), IIf(IsNull(table1.No1),0,table1.No1) AS No1
    FROM (Select * From table1
    UNION
    Select * from table2)  AS table3 LEFT JOIN table1 ON table3.code = table1.code) As table4
    left join table2 on table4.code=table2.code
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Join or Union?

    Ah, sorry, I overlooked the code.

    It will leave it blank for records that aren't in Table1 because you're reading it from Table1 and, well, it isn't there. What you want to do is read ot from Table1 if it's there and Table2 if its not, either:-
    ISULL(Table1.Code, Table2.Code)
    Or:-
    Coalesce(Table1.Code, Table2.Code)
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    350

    Re: Join or Union?

    Thanks, I hadn't thought of using isnull that way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width