GROUPIUNG SETS allows a create different groups within a single GROUP BY query
Given the following sample table:
create table t1 ( a integer, b integer, c integer ); insert into t1 (a,b,c) values (1,0,0), (0,1,0), (0,0,1), (1,0,1), (0,1,1), (1,1,1), (1,1,0), (0,0,1), (1,0,1), (0,1,1);
Then this query:
select a, b, c, count(*) from t1 group by grouping sets((a, b), (a, c));
will return:
a | b | c | count --+---+---+------ 1 | 1 | | 2 0 | 0 | | 2 1 | 0 | | 3 0 | 1 | | 3 1 | | 1 | 3 0 | | 0 | 1 1 | | 0 | 2 0 | | 1 | 4