Joining 2 Tables and Eliminating the Redundant Data

  • Hi All,

    I have a following scenario

    Table A

    usr city

    jo sf

    jo la

    Table B

    usr city

    jo nyc

    when I create a join on usr field I get a result like

    usr city usr city

    jo sf jo nyc

    jo la jo nyc

    How can I eliminate the redundant data from table 2..

    I want my result to be something like this

    usr city usr city

    jo sf jo nyc

    jo la Null Null

    Any help would be highly appreciated...

    Thanks

    Jol


    Kindest Regards,

    Joel

  • The error is in your table a

    where you are using usr jo for both row.

    This is very bad practise for join.

    Join need unique identification of row.

    For your expected result execute following statement.

    update a

    set usr = 'mo'

    where city = 'la'

    select a.usr,a.city,b.usr,b.city

    from a left outer join b on a.usr = b.usr

     

     

    Kindest Regards,

    Sameer Raval [Sql Server DBA]
    Geico Insurance
    RavalSameer@hotmail.com

  • Hey Sameer....

    Thanks for your response...

    Unfortunately thats how the data is supposed to be in table A...

    Any other workaround that...

    Putting it the other way...

    Here is my table..

    Account # SID UID

    12 234432 3

    13 234432 3

    and the way I want is

    Account # SID UID

    12 234432 3

    13 234432 -


    Kindest Regards,

    Joel

  • Jol,

    The logic here is you need to do something for

    server to identify row in unique way

    try left outer join with combination of 2 col.

    In your case Account# looks unique

    Something of this form

    Select ...

    from a left outer join b

    on a.Account = b.Account and a.SID = b.SID

    Kindest Regards,

    Sameer Raval [Sql Server DBA]
    Geico Insurance
    RavalSameer@hotmail.com

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply