Execution order of AND/OR in case when statement

Options

Hi everyone,

I am really confused about what is the order of executing AND/OR in case when statement and whether brackets work.

For example, what I want to achieve is:

(A AND B ) OR (A AND C) which means that I will select rows that can satisfy either AB or AC conditions.

(1) Question 1: Does brackets work if I have a case statement like this:

Case when (A AND B ) OR (A AND C) then 1 else 0 end.

Do I have to change to (For some reason, the following way can make my beast code too long, so I try to use the above method to put conditions in one statement):

Case

when A AND B then 1

when A AND B then 1

else 0

end

(2) Question 2:

If I have a statement like this:

Case when A AND B OR C AND D then 1 else 0 end

Does the order work like this:

((A AND B) Or C ) AND D ?

Thank you very much for your help in advance.

Best Answers

  • MichelleH
    MichelleH Coach
    Answer ✓
    Options

    @RayRay_H

    1. (A AND B ) OR (A AND C) should work for what you want to achieve, as will your case statement with multiple conditions. Grouping different conditions using parenthesis is generally best practice if you are using just one line to avoid ambiguity.
    2. We need a little more information about what rows your want to include to be able to point you in the right direction

    If you're ever unsure about how rows are being classified in case statements, I find that it's helpful to test multiple beast modes in a table card to see how each row is classified.

  • GrantSmith
    GrantSmith Coach
    Answer ✓
    Options

    Regarding question 2 it will evaluate it in the orders of parenthesis first and then AND/OR equally going left to right so you're correct in that it will evaluate it like (A AND B) Or C ) AND D

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**

Answers

  • MichelleH
    MichelleH Coach
    Answer ✓
    Options

    @RayRay_H

    1. (A AND B ) OR (A AND C) should work for what you want to achieve, as will your case statement with multiple conditions. Grouping different conditions using parenthesis is generally best practice if you are using just one line to avoid ambiguity.
    2. We need a little more information about what rows your want to include to be able to point you in the right direction

    If you're ever unsure about how rows are being classified in case statements, I find that it's helpful to test multiple beast modes in a table card to see how each row is classified.

  • GrantSmith
    GrantSmith Coach
    Answer ✓
    Options

    Regarding question 2 it will evaluate it in the orders of parenthesis first and then AND/OR equally going left to right so you're correct in that it will evaluate it like (A AND B) Or C ) AND D

    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • RayRay_H
    Options

    Thanks all for the answers and explanations.