Ryan Gomes Ryan Gomes - 1 year ago 105
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
( a.ActivityDate <s.[ExecDate] and a.ActivityDate >= s.[Date3] )
then a.ID1 =s.ID2

Answer Source

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

JOIN ... ON ((a.ID1 = s.ID2) AND ((case #1) OR (case #2)))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download