Conditional Inner Join

后端 未结 4 681
醉梦人生
醉梦人生 2021-02-05 05:12

I want to be able to inner join two tables based on the result of an expression.

What I\'ve been trying so far:

INNER JOIN CASE WHEN Reg         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 05:51

    Try putting both tables in the query using LEFT JOIN's

    LEFT JOIN TimeRegistration TR ON r.rid = TR.Id AND RegT.type =1 
    LEFT JOIN DrivingRegistration DR ON r.rid = DR.Id AND RegT.type <>1 
    

    Now, in you select clause, use

    CASE RegType.Type WHEN 1 THEN TR.SomeField ELSE DR.someField END as SomeField
    

    The other option is to use dynamic SQL

提交回复
热议问题