Using the AND with OR operator

  • Hello all,

    Using the AND with OR in the example below is not returning the intended results. I'm getting seven more records than anticipated. These seven records should not be a part of the result because they are not flagged with the criteria 'PA'

    My order of operation is not correct or my brackets are not correct I believe.

    Your insight is greatly appreciated.

    Where (t1.Serial = '00000000001' and t1.Servicedate between '201501' and '201603') or

    (t1.serial = '00000000002' and t1.Servicedate between '201512' and '201602') or

    (t1.serial = '00000000003' and t1.Servicedatebetween ' 201601' and '201609')

    and t1.AccountFlag = 'PA'

  • You need to wrap your serial/servicedate in another set of brackets

    WHERE (

    (t1.Serial = '00000000001' AND t1.Servicedate BETWEEN '201501' AND '201603') OR

    (t1.serial = '00000000002' AND t1.Servicedate BETWEEN '201512' AND '201602') OR

    (t1.serial = '00000000003' AND t1.Servicedate BETWEEN '201601' AND '201609')

    )

    AND t1.AccountFlag = 'PA'

  • This (Operator Precedence) might be helpful

    😎

  • Excellent, this worked for me. Thank you for your help.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply