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

Sql查詢計算最近一年的連續(xù)總年數(shù)

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

問題描述

限時送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

我想計算從最近一年開始的連續(xù)年份總數(shù).結(jié)果應(yīng)如下所示:

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.

現(xiàn)在檢查相等的序列并計算行數(shù):

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

根據(jù)您的零年評論:

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

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

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

相關(guān)文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應(yīng)該使用什么 SQL Server 數(shù)據(jù)類型來存儲字節(jié) [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm Does not return all data(Typeorm 不返回所有數(shù)據(jù))
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轉(zhuǎn)換為“2016-07-01 13:12:22小時格式?)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應(yīng)該返回“1?什么時候不能投射為日期?)
主站蜘蛛池模板: 久久久久国产精品一区二区 | 免费看大片bbbb欧美 | 男人的天堂中文字幕 | 91精品国产日韩91久久久久久 | 欧美一区| 欧美成视频在线观看 | 成年人网站国产 | 中文字幕国产一区 | www.99热这里只有精品 | 久久久久久a | 亚洲视频 欧美视频 | 人人看人人草 | 午夜久久久 | 天天天天天天天干 | 91精品国产92| 综合成人在线 | 国产毛片毛片 | www国产成人免费观看视频,深夜成人网 | 国产激情一区二区三区 | 色婷婷久久久亚洲一区二区三区 | 亚洲一区二区免费视频 | 国产日韩精品一区 | 日韩a级片 | 亚洲精品在线观看视频 | 日韩免费视频一区二区 | av中文字幕在线 | 成人午夜免费在线视频 | 久久精品国产99国产精品亚洲 | 日本中文字幕在线视频 | 国产精品日日夜夜 | 久久综合九九 | 在线免费黄色小视频 | 久久高清国产视频 | 精品国产乱码久久久久久丨区2区 | 精品久久久久久久久久久 | 免费毛片在线 | 爱爱视频网 | 亚洲午夜精品久久久久久app | 视频一区二区中文字幕日韩 | 亚洲成人免费在线 | 九九爱这里只有精品 |