Suppose I'm trying to put together a query with multiple conditions:
select * from t
where (Condition1 OR Condition2 OR Condition3)
My goal is to group and order the results such that:
Group 1 => C1 = true, C2 = true, C3 = true
Group 2 => C1 = true, C2 = true, C3 = false
OR C1 = true, C2 = false, C3 = true
OR C1 = false, C2 = true, C3 = true
Group 3 => C1 = true, C2 = false, C3 = false
OR C1 = false, C2 = true, C3 = false
OR C1 = false, C2 = false, C3 = true
Where C1
is Condition1
, and so on. So, if you were to convert each condition into a 1 or a 0, 1 being true 0 being false, you'd get a binary number list:
111
110
101
011
100
010
001
Note that they aren't simply ordered greatest to least.
I'm just a little curious, because I thought that normally an SQL DBMS will not bother looking at other clauses in an OR chain if the first one turns out to be true, for instance. How would I convert, record by record, the results of each condition into a number that could be sorted against?
I want the solution to be expandable to a case with N conditions, is there an elegant solution for this? It also has to be very fast. I'm heavily invested in Postgres. Is there a Postgres-specific function that will help?