Avatar of Culwatrnca11
Culwatrnca11Flag for United States of America asked on

correct placement of placement of Parentheses in a query for order of operations.

Need help with the correct placement of Parentheses.



AND eap1.proc_code IN ('98966','98967','98968','99441','99442','99443','99201','99202','99203','99204','99205','99211','99212','99213','99214','99215') AND EAP.POS_TYPE_C = '2' and px1.MODIFIERS like '%95%'

AND prc.prc_abbr LIKE ANY
(    
 'CDTAV%',
'LANGTAV%',
'MHTAV%',
'PHONE%',
'PNTAV%',
'TAV%',
'ESVV%',
'LANGVID%',
'OBVV%',
'VAV%'
)

OR eap1.proc_code IN ('98966','98967','98968','99441','99442','99443','99201','99202','99203','99204','99205','99211','99212','99213','99214','99215') AND EAP.POS_TYPE_C = '11' AND px1.MODIFIERS LIKE '%GT%'

I believe "prc.prc_abbr LIKE ANY" where I am using the wild cards could also apply to the OR portion of the code.
* TeradataCrystal Reports

Avatar of undefined
Last Comment
Culwatrnca11

8/22/2022 - Mon
ElrondCT

AND takes precedence over OR. So if you have a logical statement like

A AND B OR C AND D

it will evaluate as true if either (A AND B) is true, or (C AND D) is true. So in your case, the LIKE ANY clause would only be relevant to the first set of selections (ending with  px1.MODIFIERS like '%95%'), and if the last set of checks (after OR to the end) is true, the whole statement is true.

There's never any harm in putting in an extra set of parentheses if you're not sure how it'll be evaluated.
Mike McCracken

Agree.  Also the order then becomes important especially if the OR is to be done with only one of the other comparisons.

You have

   A AND B AND C OR D AND E

This evaluates as stated above as if the ( ) were as shown below

((A AND B) AND C) OR (D AND E)

If that is what you want then what you are using is correct.  I find most of the time the OR does need ( ) around it.

To determine the order of operations think of AND as a multiplication operator and OR as an addition operator.

All multiplications are done first working left to right then additions again working left to right.
So the ANDs are done first working left to right then the ORs also working left to right.

Another thing to consider is that Crystal will shortcut the operation if it can determine the outcome so you may want to order the operations appropriately

A AND B - Use the check that will be FALSE most of the time for A so you don't even evaluate B

Similarly for
C OR D - Use the one most likely to be TRUE for C so you don't have to evaluate D.

mlmcc
ASKER
Culwatrnca11

So here is the criteria:  

The following is for telephone codes:
98966, 98967, 98968, 98969, 99441, 99442, 99443, 99444, 99446, 99447, 99448, 99449 in combination with POS 11, 17, 19, 21, 22, 23, 31
These telephone codes are not mapped with POS 2.

The following is for video visits:
The easiest way to get info would be with POS 2, modifier’s GT and 95 and any procedure code. Claims are driven by POS 2 and/or the modifiers.

WHERE
AP.SERV_DATE BETWEEN '01/01/2018' AND CURRENT_DATE
and ap.status_c = '4'       
 AND ((EAP.POS_TYPE_C = '2') AND px1.MODIFIERS LIKE '%95%')  OR  (px1.MODIFIERS LIKE '%GT%' )  
     OR  ((eap1.proc_code) IN ('98966','98967','98968','98969','99441','99442','99443','99444','99446','99447','99448','99449')) AND (px1.POS_TYPE_C IN ('11','17','19','21','22','23','31'))

based on the above criteria, I am not getting the date result nor the ap_ap.status_c of 4 and pulling in more then the criteria i am looking for.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Mike McCracken

Try this

WHERE
 AP.SERV_DATE BETWEEN '01/01/2018' AND CURRENT_DATE
 and ap.status_c = '4'        
  AND (((EAP.POS_TYPE_C = '2') AND ((px1.MODIFIERS LIKE '%95%')  OR  (px1.MODIFIERS LIKE '%GT%' )))  
      OR  ((eap1.proc_code IN ('98966','98967','98968','98969','99441','99442','99443','99444','99446','99447','99448','99449') AND (px1.POS_TYPE_C IN ('11','17','19','21','22','23','31')))

Open in new window


mlmcc
ASKER CERTIFIED SOLUTION
ElrondCT

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Culwatrnca11

ElrondCt,

Thank you..  The last part of your response where you re did the code worked perfectly and was exactly what I was looking for. Seemed to pull in exactly what I was looking for without anything extra. Validating now but this looks good. If I need anything else will open another question.

Thank you..