我想计算一些由两个布尔列分组和未分组的列的不同出现次数

select count(Distinct some_column) as Uniques, sum(some_other_col)
from myTable T
where T.created_at>'2016-09-15'
group by T.col1, T.col2


该查询给出四个值

uniques when col1=true and col2=true
uniques when col1=true and col2=false
uniques when col1=false and col2=true
uniques when col1=false and col2=false


是否可以更改相同的查询并获得这三个值?
我无法将前4个值组合在一起

uniques  (all)
uniques when col1=true
uniques when col2=true


更新

实际上,我想保留该分组依据,因为我得到了一些其他值的总和。

最佳答案

使用条件聚合:

select count(Distinct some_column) as Uniques,
       count(distinct case when t.col1 then some_column end) as Uniques_col1_true,
       count(distinct case when t.col2 then some_column end) as Uniques_col2_true
from myTable t
where t.created_at > '2016-09-15';

08-06 21:42