久久久久久久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?什么時候不能投射為日期?)
主站蜘蛛池模板: 在线观看成人精品 | 最新国产视频 | 久久精品伊人 | 成人亚洲性情网站www在线观看 | 中文字幕亚洲视频 | 成人网视频 | 丝袜 亚洲 欧美 日韩 综合 | 成年人视频在线免费观看 | 91精品久久久久 | 中文字幕第二十页 | 精品1区| 伊人影院在线观看 | 韩日精品在线观看 | 日本精品一区二区三区视频 | 亚洲午夜精品一区二区三区他趣 | 国产中文字幕在线观看 | 欧美一级免费观看 | 亚洲一区二区三区在线 | 毛片一区二区三区 | 性精品| 国产伦精品一区二区 | 国产精品国产a级 | 日韩一级免费观看 | 欧美视频网 | 国产精品一区久久久 | 91日韩 | 99久久影院 | 黄色片视频 | 日韩欧美国产精品 | 欧美国产一区二区 | 蜜臀久久 | 高清视频一区 | 欧美日韩亚洲国产 | 久久久精品高清 | 久久九九免费 | 一区二区在线免费观看 | 91久久久久 | pacopacomama在线 | 免费看一级毛片 | 在线播放第一页 | 久久久久久久久久一区 |