Ryan Gomes Ryan Gomes - 3 months ago 19
SQL Question

JOIn by case Expression

I would like to perform this Code

select * from a
right join s
on case when s.[Diff ] = 0 and a.ActivityDate < s.[ExecDate]
then a.ID1 =s.ID2
when
( a.ActivityDate <s.[ExecDate] and a.ActivityDate >= s.[Date3] )
then a.ID1 =s.ID2
END

Answer

The case is pointless. You join the same two fields ANYWAYS, so just add your case conditions to the join condition:

SELECT ...
JOIN ... ON ((a.ID1 = s.ID2) AND ((case #1) OR (case #2)))