I’ve got two tables, both with multiple IDs, none of which are primary.
I’d like to join table2 to table1 first on ID1 if a match exists in table2. If no match exists for a given record, match on ID2; finally, if no match exists on ID2, match on ID3.
I’d started to set this up using a CASE statement similar to the following:
select *
from table1 t
inner join table2 s
on CASE
WHEN s.ID1 = t.ID1 THEN s.ID1 = t.ID1
WHEN s.ID2 = t.ID2 THEN s.ID2 = t.ID2
ELSE s.ID3 = t.ID3
END
but SQL Server doesn’t seem to like it.
Any ideas about how to set something like this up?
Result: