I am constructing a query that will naturally return a result that has values for several columns, as well as some integer-valued grouping columns (indicating whether a particular value column was subtotalled but that's not in itself relevant to my question). I want to put a constraint on this query's grouping columns, that only accepts rows where grouping column n is non-zero if all the grouping columns 1 - (n-1) are also non-zero.
It seems like there ought to be a nice way to express this in SQL, but all I can come up with so far is the rather cumbersome
where ((grp1 = 0 AND grp2 = 0 AND grp3 = 0 AND ... AND grpn = 0)
or (grp1 = 1 AND grp2 = 0 AND grp3 = 0 AND ... AND grpn = 0)
or (grp1 = 1 AND grp2 = 1 AND grp3 = 0 AND ... AND grpn = 0)
...
or (grp1 = 1 AND grp2 = 1 AND grp3 = 1 AND ... AND grpn = 1)
which is harder to understand the concept from if you're reading it, looks like it would be harder for a SQL engine to come up with a decent optimisation, and will generally just clog up logs as well, burying the important bits of the SQL in noise.
Since this concept seems not too unusual, I'm hoping there is a better way to express it that will generally be more concise and hopefully will be more performant on the server itself (assuming my naive version isn't going to be that efficient).