We help IT Professionals succeed at work.

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

Culwatrnca11
Culwatrnca11 asked
on
121 Views
Last Modified: 2018-02-15
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.
Comment
Watch Question

CERTIFIED EXPERT

Commented:
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 McCrackenSenior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013

Commented:
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
Culwatrnca11Data Analyst

Author

Commented:
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.
Mike McCrackenSenior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013

Commented:
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
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Culwatrnca11Data Analyst

Author

Commented:
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..
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.