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'
Culwatrnca11Data AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.