SQL Server : conditional (IF-ELSE) on Left Outer Join -


i have scenario want put condition on join, i.e if = b join on 1 set else join on another. tables in both scenario same conditions different. have tried using case syntax error.

select *  [table1] cup left outer join table2 cp on cup.statecode = cp.statecode                            , (cup.clientid = cp.clientid or cp.clientid = 0) 

what have tried

select *  [table1] cup left outer join table2 cp           on case                 when cup.clientid =  cp.clientid                     cup.statecode = cp.statecode , cup.clientid =  cp.clientid                    else cup.statecode = cp.statecode , (cup.clientid <>  cp.clientid or cp.clientid = 0) 

@juan

 on     cup.statecode = cp.statecode , ((cup.clientid =  cp.clientid or cp.clientid <> 0) or (cup.clientid <> cp.clientid or cp.clientid = 0)) 

  • you'll have use coalesce select clause
  • note cp1.clientid null removes gordon solution's limitation regarding nulls

select  ...                     table1  cup          left outer join table2  cp1          on              cp1.statecode = cup.statecode                       , cp1.clientid  = cup.clientid           left outer join table2  cp2          on              cp1.clientid null                      , cp2.statecode = cup.statecode                      , cp2.clientid  = 0 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -