Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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 2012中的行数?_Sql_Sql Server - Fatal编程技术网

如何计算SQL Server 2012中的行数?

如何计算SQL Server 2012中的行数?,sql,sql-server,Sql,Sql Server,我试图找出一个人(id=A3)是否在给定的一年(2013年)内至少五个月或更长时间内持续活跃在某个项目中。如有任何建议,将不胜感激。我的数据如下所示: 您只需使用分组依据和条件表达式: select id, (case when count(ActiveMonthYear) >= 5 then 'YES!' else 'NAW' end) from table t where ListOfTheMonths between '201301' and '201312' grou

我试图找出一个人(id=A3)是否在给定的一年(2013年)内至少五个月或更长时间内持续活跃在某个项目中。如有任何建议,将不胜感激。我的数据如下所示:


您只需使用
分组依据和条件表达式:

select id,
       (case when count(ActiveMonthYear) >= 5 then 'YES!' else 'NAW' end)
from table t
where ListOfTheMonths between '201301' and '201312'
group by id;
编辑:

我想“连续”不只是指任何五个月。为此,有多种方法。我喜欢行号差异的方法

select distinct id
from (select t.*,
             (row_number() over (partition by id order by ListOfTheMonths) -
              count(ActiveMonthYear) over (partition by id order by ListOfTheMonths)
             ) as grp
      from table t
      where ListOfTheMonths between '201301' and '201312'
     ) t
where ActiveMonthYear is not null
group by id, grp
having count(*) >= 5;
对于连续活动月份的组,子查询中的差异是恒定的。然后将其用于分组。结果是满足此条件的所有ID的列表。您可以为特定id在其中添加
(在子查询中执行)


顺便说一下,这是使用
选择distinct
group By
编写的。这是其中一个罕见的情况下,这两个适当地使用在一起。一个
id
可以在同一年中有两个为期五个月的时段。没有理由在结果集中包含该人员两次。

您的数据看起来如何?你忘了在问题中加上这个吗?你所说的连续是什么意思?嗨,戈登,连续意味着至少五个月没有月间隔。@Gordon.这里的顺序不重要吗?这可能是正确的答案。不过我想。也许用户需要连续登录5个月?@vkp。当我第一次回答这个问题时,我误解了它。