there is a question floated. It’s set in basic SQL terms, but its nature is pure math (so maybe I should visit https://mathoverflow.net too).
I have a table in some theoretical database with 6 fields, all are numbers. Also we have basic conditions, such as Field_1 > Field_5, Field_4 = 3 etc., 7 conditions total. I need to write a select, which satisfies at least 4 of them.
Writing long select with many logical conditions such as (cond_1 AND cond_2 AND cond_3 and cond_4) OR (…) is not a way, because 4-combination from 7 elements is equal to 140, and one doesn’t want to write so many conditions.
So how do I write a select in its simplified form?
One way of doing it is to count 1 for each condition that the row satisfies and compare the sum to your target value:
Note that this will require evaluating all the conditions for each row so you won’t get a short-circuiting effect, but it’s simple and maybe it has good enough performance for you.
If you’re using MySQL you can write this in a much simpler way because a boolean result is equivalent to 0 or 1 so you don’t need the CASE statements: