Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.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
用于获取基于同一表中2个条件的计数的单个SQL查询_Sql_Sql Server_Dml - Fatal编程技术网

用于获取基于同一表中2个条件的计数的单个SQL查询

用于获取基于同一表中2个条件的计数的单个SQL查询,sql,sql-server,dml,Sql,Sql Server,Dml,我有这样的数据 现在,我需要一个查询来获取id计数,其中信息为“是”,id计数同时为“是”和“否” 单一查询: SELECT COUNT(id) FROM table WHERE info = 'yes' 及 自 您可以使用聚合和窗口函数完成此操作: SELECT DISTINCT SUM(MAX(CASE WHEN info = 'yes' THEN 1 ELSE 0 END)) OVER () id_as_yes, COUNT(CASE WHEN COUNT(

我有这样的数据

现在,我需要一个查询来获取id计数,其中信息为“是”,id计数同时为“是”和“否”

单一查询:

SELECT COUNT(id) FROM table WHERE info = 'yes'


您可以使用聚合和窗口函数完成此操作:

SELECT DISTINCT 
       SUM(MAX(CASE WHEN info = 'yes' THEN 1 ELSE 0 END)) OVER () id_as_yes,
       COUNT(CASE WHEN COUNT(DISTINCT info) = 2 THEN 1 END) OVER () id_as_yes_no
FROM tablename
GROUP BY id
看。 结果:


您需要条件聚合

Select id, 
       Count(case when info = 'y' then 1 end) as y_count,
       Count(case when info = 'y' and has_n = 1 then 1 end) as yn_count
  From (SELECT id, info,
               Max(case when info = 'no' then 1 end) over (partirion by id) as has_n
         From your_table) t

您可以在不使用子查询的情况下执行此操作。这取决于观察到的不仅仅是以下ID的数量:

count(distinct id) - count(distinct case when info = 'yes' then id end)
同样,赞成的人数也是如此。所以,两者都有的数字是id的数量减去no only的数量减去yes only的数量:

select count(distinct case when info = 'yes' then id end) as num_yeses,
       (count(distinct id) -
        (count(distinct id) - count(distinct case when info = 'yes' then id end)) -
        (count(distinct id) - count(distinct case when info = 'no' then id end))
       )
from t;
    

这应该可以做到这一点…它肯定不是高效或优雅的,但没有空值聚合警告


你的预期结果是什么?既然Id having Yes是71,2,3,4,5,6,7,Id having and Yes and No两个变量只有31,4,6,它应该给我Id_as_Yes=7和Id_as_Yes_No=3这部作品!感谢@forpas没有工作,而且在600万数据上执行速度太慢
Select id, 
       Count(case when info = 'y' then 1 end) as y_count,
       Count(case when info = 'y' and has_n = 1 then 1 end) as yn_count
  From (SELECT id, info,
               Max(case when info = 'no' then 1 end) over (partirion by id) as has_n
         From your_table) t
count(distinct id) - count(distinct case when info = 'yes' then id end)
select count(distinct case when info = 'yes' then id end) as num_yeses,
       (count(distinct id) -
        (count(distinct id) - count(distinct case when info = 'yes' then id end)) -
        (count(distinct id) - count(distinct case when info = 'no' then id end))
       )
from t;
    
Select
    (select select count(distinct id) from mytest where info = 'yes') as yeses
    ,(select count(distinct id) from mytest where info = 'no' and id in (select distinct id from mytest where info = 'yes' )) as [yes and no]