Mysql Sql distinct和group by布尔字段

Mysql Sql distinct和group by布尔字段,mysql,sql,Mysql,Sql,我想计算由两个布尔列分组和非分组的某些列的不同出现次数 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=fa

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

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';

使用条件聚合:

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';
试试这个

SELECT SUM(CASE WHEN col1 = TRUE
           AND col2 = TRUE THEN 1 ELSE 0 END) AS opt1,
       SUM(CASE WHEN col1 = TRUE
           AND col2 = FALSE THEN 1 ELSE 0 END) AS opt2,
FROM
    (SELECT DISTINCT col1,
                     col2,
                     somecolumn
     FROM TABLE T
     WHERE ...) AS TB
试试这个

SELECT SUM(CASE WHEN col1 = TRUE
           AND col2 = TRUE THEN 1 ELSE 0 END) AS opt1,
       SUM(CASE WHEN col1 = TRUE
           AND col2 = FALSE THEN 1 ELSE 0 END) AS opt2,
FROM
    (SELECT DISTINCT col1,
                     col2,
                     somecolumn
     FROM TABLE T
     WHERE ...) AS TB

@阿尔伯特。不,它给出了你要的三个。但是,您可以添加其他条件以获得所需的所有值。@albert。不,它给出了你要的三个。但是,您可以添加其他条件以获得所需的所有值。