Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 根据where语句从子查询中选择多个结果计数?_Sql - Fatal编程技术网

Sql 根据where语句从子查询中选择多个结果计数?

Sql 根据where语句从子查询中选择多个结果计数?,sql,Sql,因此,我从子查询中进行选择,并根据使用where语句创建的列中的值是否包含值来显示这些行的计数 我想创建一个查询,根据不同的where语句返回几个计数 我的表有两列:ID1和abc2 select count(*) from ( select `id1`, count(distinct(`abc2`)) as total_abc from TABLE where `id1` != 'Unknown' and `abc2` != 'NULL' group by `id1` order by tot

因此,我从子查询中进行选择,并根据使用where语句创建的列中的值是否包含值来显示这些行的计数

我想创建一个查询,根据不同的where语句返回几个计数

我的表有两列:ID1和abc2

select count(*) from
(
select `id1`, count(distinct(`abc2`)) as total_abc
from TABLE
where `id1` != 'Unknown' and `abc2` != 'NULL'
group by `id1`
order by total_abc desc
)
where total_abc = 1




select count(*) from
(
select `id1`, count(distinct(`abc2`)) as total_abc
from TABLE
where `id1` != 'Unknown' and `abc2` != 'NULL'
group by `id1`
order by total_abc desc
)
where total_abc = 2





select count(*) from
(
select `id1`, count(distinct(`abc2`)) as total_abc
from TABLE
where `id1` != 'Unknown' and `abc2` != 'NULL'
group by `id1`
order by total_abc desc
)
where total_abc = 3
所需的结果只是将以下几个查询组合在一起:

count1 | count2 | count3
__________________________
123    | 222     | 34567

您可以使用两个级别的聚合:

select
    count(case when num_vals = 1 then 1 end) as val_1,
    count(case when num_vals = 2 then 1 end) as val_2,
    count(case when num_vals = 3 then 1 end) as val_3
from
(
    select id1, count(distinct abc2) as num_vals
    from TABLE
    where id1 <> 'Unknown' and abc2 <> 'NULL'
    group by id1
) t;
您似乎想要列中的值。我更喜欢成排排列:

select num_vals, count(*)
from (select id1, count(distinct abc2) as num_vals
      from TABLE
      where id1 <> 'Unknown' and abc2 <> 'NULL'
      group by id1
     ) t
group by num_vals
order by num_vals;

因为您的子查询是完全相同的,所以您可以这样做

select 
SUM(CASE WHEN total_abc = 1 THEN 1 ELSE 0 END) Count1
SUM(CASE WHEN total_abc = 2 THEN 1 ELSE 0 END) Count2
SUM(CASE WHEN total_abc = 3 THEN 1 ELSE 0 END) Count3
 from
(
select `id1`, count(distinct(`abc2`)) as total_abc
from TABLE
where `id1` != 'Unknown' and `abc2` != 'NULL'
group by `id1`
order by total_abc desc
)
where total_abc IN (1,2,3)

请用您正在使用的数据库标记您的问题。嗨,如果我想对所有值执行此操作,该怎么办?所以1-无穷大?如果你想让它以列的形式返回,可能很难做到,除非你是透视的。我建议看看上面Gordon的答案,他使用分组来实现相同的结果,但是成排的。