• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 64
  • Last Modified:

Using AND and OR

I can not get this to work out right. Not sure if I am putting the Parenthases in the right place.

basically I want to pull back everything that matches with the Proc_codes and POS_TYPE_C along with POS_Type_C = 11,17,19~ for the codes listed.

then only if  POS_Type_C =2 is found.

Then lastly POS_TYPE_C in 11,49, 50~ with a modifier of 95.

What is the proper way to setup these line?


AND eap1.proc_code IN ('98966','98967','98968','99441','99442','99443') AND (eap1.proc_code BETWEEN 99201 AND 99205) AND EAP.POS_TYPE_C IN ('11','17','19','21','22','23','31')  OR EAP.POS_TYPE_C = '2' OR EAP.POS_TYPE_C IN ('11','49','50','71','72') AND modifier = '95'
0
Culwatrnca11
Asked:
Culwatrnca11
1 Solution
 
ManjuIT - Project ManagerCommented:
Try

eap1.proc_code IN ('98966','98967','98968','99441','99442','99443') AND eap1.proc_code BETWEEN '99201' AND '99205' AND EAP.POS_TYPE_C IN ('11','17','19','21','22','23','31')  and EAP.POS_TYPE_C = '2' AND modifier = '95' OR  eap1.proc_code IN ('98966','98967','98968','99441','99442','99443') AND eap1.proc_code BETWEEN '99201' AND '99205' AND EAP.POS_TYPE_C IN ('11','17','19','21','22','23','31')  and EAP.POS_TYPE_C IN ('11','49','50','71','72')
0
 
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
To me, I find it helps a lot if we try to write the query in a way similar to how you explained the problem. Hence, the following should work.

AND (( (eap1.proc_code IN ('98966','98967','98968','99441','99442','99443') 
        OR (eap1.proc_code BETWEEN 99201 AND 99205)) 
       AND EAP.POS_TYPE_C IN ('11','17','19','21','22','23','31')
     )
     OR (EAP.POS_TYPE_C = '2')
     OR (EAP.POS_TYPE_C IN ('11','49','50','71','72') AND modifier = '95')
    )

Open in new window


For the 1st condition, you would want the proc_code to be any of the specified values. Hence, the OR. Similarly, if you apply an OR without the brackets, it applies to the entire set of conditions. Hence, the brackets. Hope it helps!
1
 
Scott PletcherSenior DBACommented:
You shouldn't mix string and numeric comparisons against the same column.
But, I agree, properly grouping the conditions makes them much easier to verify and/or change.

AND (
    (eap1.proc_code IN ('98966','98967','98968','99201','99202','99203','99204','99205','99441','99442','99443')
     AND EAP.POS_TYPE_C IN ('11','17','19','21','22','23','31'))
    OR
    (EAP.POS_TYPE_C = '2')
    OR
    (EAP.POS_TYPE_C IN ('11','49','50','71','72')
     AND modifier = '95')
    )
1
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Culwatrnca11Data AnalystAuthor Commented:
Nakul,

I get an error message. Report tried to run then I got a 2620: The format or data contains a bad character. Never seen that before.
0
 
Nakul VachhrajaniTechnical Architect, Capgemini IndiaCommented:
It's probably a special character that sneaked in as I copied it over from SQL Server Operations Studio. Can you try using this (I just pasted into notepad and back)? If it still errors out, please try to run the query manually in SSMS and post the output error message.

AND (((eap1.proc_code IN ('98966','98967','98968','99441','99442','99443') 
        OR (eap1.proc_code BETWEEN 99201 AND 99205)) 
       AND EAP.POS_TYPE_C IN ('11','17','19','21','22','23','31')
      )
     OR (EAP.POS_TYPE_C = '2')
     OR (EAP.POS_TYPE_C IN ('11','49','50','71','72') AND modifier = '95')
    )

Open in new window

0
 
Olaf DoschkeSoftware DeveloperCommented:
eap1.proc_code IN ('98966','98967','98968','99441','99442','99443') AND (eap1.proc_code BETWEEN 99201 AND 99205)

You're having two clauses on eap1.proc_code, once with a list of strings, once with a range of integers.
Also, you combine them with AND.

Implicit conversion might make the different types work, but a number can't be in the range of 99201 to 99205 and then also be one of ('98966','98967','98968','99441','99442','99443') at the same time.

In short where clauses like a=1 and a=2 will not give any results, as any a can't be 1 and 2 at the same time. Notice the whole expression is evaluated for all rows, but one row at a time. So it's not stopping if one condition matches and then checks the other at other rows...

Bye, Olaf.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now