Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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 Server满足条件时,统计每个组中的行数_Sql_Sql Server_Tsql_Date_Group By - Fatal编程技术网

当Sql Server满足条件时,统计每个组中的行数

当Sql Server满足条件时,统计每个组中的行数,sql,sql-server,tsql,date,group-by,Sql,Sql Server,Tsql,Date,Group By,我的桌子如下所示: ID Date IsFull 1 2020-01-05 0 1 2020-02-05 0 1 2020-02-25 1 1 2020-03-01 1 1 2020-03-20 1 我想显示ID=1的月份数 给定

我的桌子如下所示:

         ID            Date     IsFull
         1          2020-01-05    0 
         1          2020-02-05    0  
         1          2020-02-25    1  
         1          2020-03-01    1
         1          2020-03-20    1
我想显示ID=1的月份数

给定月份的总和(isfull)/计数(*)大于.6(该月超过60%的次数isfull=1)

所以最终的输出应该是

 ID     HowManyMonths
  1       1     --------(Only month 3----2 out 2 cases)
如果问题变为总和(isfull)/计数(*)>。4

那么最终的输出应该是

  ID     HowManyMonths
   1       2     --------(Month 2 and Month 3)

谢谢

您可以通过两个级别的聚合来实现这一点:

select id, count(*) howManyMonths
from (
    select id
    from mytable
    group by id, year(date), month(date)
    having avg(1.0 * isFull) > 0.6
) t
group by id
子查询按id、年份和月份进行聚合,并使用
having
子句来筛选满足成功率的组(
avg()
)。外部查询统计每个id超过目标速率的月份数