久久久久久久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?什么時候不能投射為日期?)
主站蜘蛛池模板: 欧美精品久久久久久 | 亚洲区中文字幕 | 免费在线观看91 | 亚洲精品不卡 | 国产一区不卡在线观看 | 久久久久国产一区二区三区 | 一区二区免费在线观看 | 亚洲黄色av | 欧美国产精品一区二区 | 成人激情免费视频 | 人人澡人人射 | 久久新视频 | 一级无毛片 | 国产欧美日韩精品在线观看 | 国产精品美女久久久久久久久久久 | 91在线免费视频 | 亚洲天堂一区二区 | 亚洲免费一区二区 | 日韩毛片在线观看 | 色天堂视频 | 国产一区二区在线免费观看 | 日韩欧美在线免费观看 | 99热这里有精品 | 欧美一级免费看 | 欧美不卡网站 | 欧美福利久久 | 亚洲一区二区三区高清 | 一区二区三区精品视频 | 在线观看毛片网站 | 精品一区久久 | 国产精品久久久久久238 | 国产精品麻| 精品国产黄a∨片高清在线 www.一级片 国产欧美日韩综合精品一区二区 | 亚洲精品视频在线观看免费 | 日韩中文字幕视频在线 | 国产色黄 | 女人毛片a毛片久久人人 | 亚洲国产黄 | 亚洲精品乱码久久久久v最新版 | 国产精品自产拍在线观看蜜 | 国产乱码精品一区二区三区中文 |