Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.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/7/sql-server/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查询_Sql_Sql Server_Tsql - Fatal编程技术网

从最近一年开始计算连续年份总数的Sql查询

从最近一年开始计算连续年份总数的Sql查询,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有一张临时桌: CREATE TABLE Temp ( [ID] [int], [Year] [INT], ) **ID Year** 1 2016 1 2016 1 2015 1 2012 1 2011 1 2010 2 2016 2 2015 2 2014 2 2012 2 2011 2 2010 2 2009 3 2016 3 2015 3 2004 3 1999 4 2016 4 2015

我有一张临时桌:

 CREATE TABLE Temp 
( 
  [ID]  [int],
  [Year]  [INT],
 )
**ID    Year**
1 2016
1   2016
1   2015
1   2012
1   2011
1   2010
2   2016
2   2015
2   2014
2   2012
2   2011
2   2010
2   2009
3   2016
3   2015
3   2004
3   1999
4   2016
4   2015
4   2014
4   2010
5   2016
5   2014
5   2013
我想计算从最近一年开始的连续年份总数。 结果应该如下所示:

ID  Total Consecutive Yrs
1   2
2   3
3   2
4   3
5   1

可以使用窗口函数执行此操作:

select id, count(distinct year)
from (select t.*,
             dense_rank() over (partition by id order by year + seqnum desc) as grp
      from (select t.*,
                   dense_rank() over (partition by id order by year desc) as seqnum
            from temp t
           ) t
     ) t
where grp = 1
group by id;

这假设最近一年是按id计算的。

您可以使用lead并获得如下计数:

Select top (1) with ties Id, RowN as [Total Consecutive Years] from (
    Select *, Num = case when ([year]- lead(year) over(partition by Id order by [Year] desc) > 1) then 0 else 1 end 
        , RowN = Row_Number() over (partition by Id order by [Year] desc)
    from temp
) a
where a.Num = 0
order by row_number() over(partition by Id order by RowN)
+----+-------------------------+
| Id | Total Consecutive Years |
+----+-------------------------+
|  1 |                       2 |
|  2 |                       3 |
|  3 |                       2 |
|  4 |                       3 |
|  5 |                       1 |
+----+-------------------------+
输出如下:

Select top (1) with ties Id, RowN as [Total Consecutive Years] from (
    Select *, Num = case when ([year]- lead(year) over(partition by Id order by [Year] desc) > 1) then 0 else 1 end 
        , RowN = Row_Number() over (partition by Id order by [Year] desc)
    from temp
) a
where a.Num = 0
order by row_number() over(partition by Id order by RowN)
+----+-------------------------+
| Id | Total Consecutive Years |
+----+-------------------------+
|  1 |                       2 |
|  2 |                       3 |
|  3 |                       2 |
|  4 |                       3 |
|  5 |                       1 |
+----+-------------------------+
e、 g.对于ID=1:

1   2016    1   1
1   2015    2   2
1   2012    5   3
1   2011    6   4
1   2010    7   5
只要没有间隙,两个序列增加的幅度相同

现在检查相等的序列并计算行数:

with cte as 
 (
   select ID,
      -- returns a sequence without gaps for consecutive years
      first_value(year) over (partition by ID order by year desc) - year + 1 as x, 
      -- returns a sequence without gaps
      row_number() over (partition by ID order by year desc) as rn
   from Temp

 ) 
select ID, count(*)
from cte
where x = rn  -- no gap
group by ID
编辑:

根据您的“零年评论”:


但是ID 1已经连续两年,两次。。。你是怎么处理的?另外,您尝试了什么?没有按预期工作,只需为每个ID返回最近一年的一行并计算该行,结果总是1。@dnoeth。是的,那是一个不完整的想法。现在应该是固定的。当年份为零时,结果也应该为零。但是在这个查询中,它返回了一个1,这是一个错误。你说的年份为零是什么意思?你的样品和预期结果中没有这样的东西。是的,我的错,我忘了把这个添加到样品中。我的意思是当ID=6,Year=0时。假设这一行也添加到上述示例中。那么结果应该是id=0,连续年份总数=0。@genie:但是一年零意味着什么?我只是再次生成样本我猜:id年份1 2016 1 2015 1 2012 1 2011 1 2010 2 2016 2 2015 2 2014 2 2012 2 2011 2 2010 2 2009 3 2016 3 2015 3 2004 3 1999 4 2016 4 2014 4 2010 5 2014 5 2013 6 0结果集应如下所示:ID Total Continuous Yrs 1 2 3 3 2 4 3 5 1 6 0Crazy logic:-但当只有一个ID和一行时,它会失败。