November 6, 2013 at 12:44 pm
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?
November 6, 2013 at 12:48 pm
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
November 6, 2013 at 1:26 pm
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