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.
Culwatrnca11Data AnalystAsked:
Who is Participating?
 
ElrondCTConnect With a Mentor Commented:
I've rewritten your match construct to align all operators by priority, with line numbers to make it easier to reference:

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


Since OR is the last to be processed, this structure means that whenever (px1.MODIFIERS LIKE '%GT%' )  is True, your record will be selected (because there's an OR on either side of it). That's probably not what you want.

If you want the checks on AP.SERV_DATE and ap.status to be required to match for all records, then put an open parenthesis just after the AND on line 4, with a matching close parenthesis at the end of the whole selection. But that may not be sufficient. I'm not sure how your other clauses interact, but I'm guessing the following situations are matches.
* If EAP.POS_TYPE_C = '2' plus either px1.MODIFIERS LIKE '%95%' OR  px1.MODIFIERS LIKE '%GT%'
* If eap1.proc_code in the list of 5-digit numbers, plus px1.POS_TYPE_C in the list of 2-digit numbers

If that paragraph describes your logic, then the selection (with redundant parentheses removed) would be:

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')))
1
 
ElrondCTCommented:
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.
0
 
mlmccCommented:
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
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Culwatrnca11Data AnalystAuthor 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.
0
 
mlmccCommented:
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
0
 
Culwatrnca11Data AnalystAuthor 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..
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.

All Courses

From novice to tech pro — start learning today.