0

I have a table returning me a series of boolean(0/1) paired with an operator (and/or) as follow

e.g.

ID Operator Value
1 and 0
2 and 0
3 or 1

I am trying to find an efficient way to parse these rows into a single result that would be equivalent to

(false and false or true) //returning true

I think this would be possible with CLR, but it appear to be something I could do in regular T-SQL. I've thought about scalar function receiving a table variable. Would this be my best course of action at this stage?

The table will never be very complex and always start with an AND operator which can be ignored.

As pointed out in comment, I was going to do this with a simple AND and if I have any OR it would shortcut the entire thing, but I am doubting my own self as when I tried this in JS it doesn't appear to be the case: https://jsfiddle.net/3ctshxae/

Edit: I incorrectly stated I would process them in as a stack since that is what made sense to me initially. I never thought about orders of operators without brackets as one would simply never write them that way when programming. I indeed would like to respect precedence rules of Boolean logic of ANDs taking precedence.

I know I could do this in a CLR and simply throw the logic in it and get the returned value, but I was hoping to avoid stepping outside of T-SQL.

10
  • You order by the ID? Commented Jul 13, 2021 at 22:21
  • 3
    Should all ANDs be applied before ORs or they should be processed in a stack manner? Commented Jul 13, 2021 at 22:22
  • Could be any number of rows? And you never have brackets? Cos without brackets your ORs will always override everything else. Commented Jul 13, 2021 at 22:23
  • 1
    @DaleK Assuming we go for the standard "and precedes or" I can't see a way to do this without recursion, and I think that's never going to perform as well as a simple while loop. Can't believe I've just said that. Commented Jul 13, 2021 at 23:02
  • 1
    I wasn't being quite correct when I said "OR" shortcuts it, the actual rules are "When more than one logical operator is used in a statement, the AND operators are evaluated first." which means order is important. Commented Jul 13, 2021 at 23:17

1 Answer 1

2

Normally one would process this using precedence rules: and comes before or, and this would complicate things immensely.

But if you want to process this in a very simple stack manner, where each item is processed in order, you can rearrange this in the following manner.

  • Since (A AND B) OR C is the same as C OR (A AND B), we can simply look for all ands separately from or
  • We can ignore any or condition which is false because any other condition will override it
  • We need all and conditions to be true
SELECT CASE WHEN
        (COUNT(CASE WHEN Operator = 'and' AND value = 1 THEN 1 END) > 0
     AND COUNT(CASE WHEN Operator = 'and' AND value = 0 THEN 1 END) = 0)
  OR COUNT(CASE WHEN Operator = 'or' AND value = 1 THEN 1 END) > 0
  THEN 1 ELSE 0 END
FROM YourTable;

You say that actually you do want to use precedence rules. You have no brackets, so that makes things easier.

So we can formulate it like this:

  • Any or which is false can be ignored
  • Any or which is true overrides anything else
  • Group all ands together in islands, and evaluate each island separately
  • Any island of all trues is true, all others are false
SELECT CASE WHEN
  EXISTS (SELECT 1
    FROM YourTable t
    WHERE Operator = 'or' AND value = 1)
  OR EXISTS (SELECT 1
    FROM (
        SELECT *,
            ROW_NUMBER() OVER (ORDER BY ID) - 
            ROW_NUMBER() OVER (PARTITION BY Operator ORDER BY ID) grouping
        FROM YourTable t
    ) t
    WHERE Operator = 'and'
    GROUP BY grouping
    HAVING COUNT(CASE WHEN value = 0 THEN 1 END) = 0
  )
 THEN 1 ELSE 0 END;
Sign up to request clarification or add additional context in comments.

4 Comments

I was incorrect in my question and the comment made me realize I do indeed intend to process them with the precedence rules intact. Edited the question.
@jfrobishow OK I've given you a solution for that also
that works very well, thank you! I have never seen a grouping done with a calculation like you're doing. I think it's pairing the "islands" is that it?
Yes it's a technique known as "gaps and islands"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.