Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 SQL AND OR QUESTION

Author  Topic 

Blessed1978
Yak Posting Veteran

97 Posts

Posted - 2015-01-27 : 19:28:33
I have the follwing in my where clause of a huge statement
am i using the AND & OR in the correct format? are my parenthesis in the correct location (
?

WHERE d.customer flag = 0
AND d.customer_type = 'Individual'
AND (c.cust_key in (select custkey from employee where Location IN ('italy',germany'))
or (c.cst_key in (vw_client_location where email not in (select email from vw_expired ))
or emp_country in (select country from Lookup_countries)
or (c.custkey In (Select abc.custkey from vw_test Where delete_flag = 0)))

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-01-27 : 21:40:26
the format looks ok but perhaps you mean to group all the ors together with the last and. In that case put an open parentheses after the key word AND and a close parentheses at the end of the last or condition
Go to Top of Page

viggneshwar
Yak Posting Veteran

86 Posts

Posted - 2015-01-28 : 02:31:03
1. Based on your requirement use parenthesis between and & or conditions

2. cust_key is not to be used multiple times it can be used like below

WHERE d.customer flag = 0
AND d.customer_type = 'Individual'
AND ( (c.cust_key in (select custkey from employee where Location IN (''italy',germany')
union Select abc.custkey from vw_test Where delete_flag = 0
union Select custkey from vw_client_location where email not in (select email from vw_expired )

or emp_country in (select country from Lookup_countries)
)
Go to Top of Page
   

- Advertisement -