Multiple Column Joins

  • I have two tables and I need to join on one column if there is a match else join on another

    Example

    SELECT

    tb1.Col1

    , tb2.Col2

    , tb2.Col3

    FROM TB1

    LEFT OUTER JOIN TB2

    ON tb1.Col1 = tb2.Col1

    OR tb1.Col2 = tb2.Col2

    Problem is, using OR the performance is horrible, what is a more efficient way of doing this?

  • you could use a union to do the same thing:

    select tb1.Col1, tb2.Col2, tb2.Col3

    from TB1

    left outer join TB2 on tb2.Col1 = tb2.Col1

    union

    select tb1.Col1, tb2.Col2, tb2.Col3

    from TB1

    left outer join TB2 on tb2.Col2 = tb2.Col2



    Microsoft Certified Master - SQL Server 2008
    Follow me on twitter: @keith_tate

    Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • That is perfect!! Before, the query that I was running took over 6 minutes to run. This one ran seconds. Thank you very much!!!

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

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