sql server - More efficient way to write query -


i have 2 fields email address in #data table trying join on. want join on rep email address , if doesn't work, want join on email. tried running following query:

select a.* #data join #email b on b.email=coalesce(a.rep_email_address,a.email)  a.rep_email_address<>a.email 

this doesn't work, because in case a.rep_email_adress not null doesn't match b.email, drop record instead of taking a.email field.

this work-around found:

select a.* #data join #email b on a.email=b.email except  select a.* #data join #email b on a.rep_email_address=b.email union select a.* #data join #email b on a.rep_email_address=b.email  a.rep_email_address<>a.email 

this however, far optimal, wondering- way write perform better/look cleaner or simpler? clarify- query works (the second query), wondering if there better way write it.

thank you!

this should simpler. however, recommend check execution plan on query analyze if more optimal. [or compare resulting execution times on tables]

select a.*    #data        join #email b             on (a.rep_email_address = b.email                or a.email = b.email ) a.rep_email_address<>a.email;  # not sure why or if need clause specifically. 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -