久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Sql查詢計算最近一年的連續總年數

Sql query to Count Total Consecutive Years from latest year(Sql查詢計算最近一年的連續總年數)
本文介紹了Sql查詢計算最近一年的連續總年數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我有一個臨時表:

 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

我想計算從最近一年開始的連續年份總數.結果應如下所示:

I want to calculate the total consecutive years starting from the most recent Year. Result should look like this:

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

推薦答案

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

例如對于 ID=1:

1   2016    1   1
1   2015    2   2
1   2012    5   3
1   2011    6   4
1   2010    7   5

只要沒有間隙,兩個序列的增加都是一樣的.

As long as there's no gap, both sequences increase the same.

現在檢查相等的序列并計算行數:

Now check for equal sequences and count the rows:

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

根據您的零年評論:

with cte as 
 (
   select ID, year,
      -- 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, 
   -- remove the year zero from counting
   sum(case when year <> 0 then 1 else 0 end)
from cte
where x = rn
group by ID

這篇關于Sql查詢計算最近一年的連續總年數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm Does not return all data(Typeorm 不返回所有數據)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
How to convert #39;2016-07-01 01:12:22 PM#39; to #39;2016-07-01 13:12:22#39; hour format?(如何將“2016-07-01 01:12:22 PM轉換為“2016-07-01 13:12:22小時格式?)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
主站蜘蛛池模板: 超碰人人干 | 欧美精品xxx| 免费av不卡| 日韩一区二区三区在线 | 不卡免费视频 | 天天插天天插 | 黄色小说视频网站 | 国产精品资源 | 日本精品网站 | 久草福利资源 | 成年人的免费视频 | 亚洲精品久久久蜜桃 | 欧美色综合网 | av片在线看 | 中文字幕第一 | 亚洲a视频| 亚洲va国产va天堂va久久 | 九色精品 | 日本天堂网| 黄色片视频在线观看 | 婷婷丁香激情 | 亚洲一区二区欧美 | www亚洲天堂 | 91蜜桃婷婷狠狠久久综合9色 | 丰满少妇在线观看网站 | 亚洲综合日韩 | 欧美综合一区二区 | 日本中文字幕在线播放 | 在线免费观看黄色片 | 极品白嫩少妇无套内谢 | 一级黄色片免费看 | 亚洲成a人片| 国产欧美日韩一区二区三区 | 精品在线一区 | 四虎黄色网址 | 一级黄色片免费 | 99久久久国产精品 | 免费一区二区 | 韩国黄色网址 | 亚洲精品国产精品国自产观看浪潮 | 久久久噜噜噜 |