這是一個網友在PASS社團問的問題,個人筆記一下

以下是我簡單建立資料表的Code
Create Table t1(Amort int,Type int);
Go
Insert Into t1(Amort,Type)
Values
(1,2),(2,3),(3,3),(4,4),(5,4),
(6,1),(7,2),(8,4),(9,4),(10,4),
(11,3);
GO
資料如下圖所示

接下來執行取連續範圍的語法
With tb as(
SELECT Amort, Amort - ROW_NUMBER() OVER(ORDER BY Amort) AS Num
FROM t1 Where [type]=4
)
Select * From tb Where Num = (Select max(Num) From tb);
GO
執行結果如下圖所示

上面會取出Type=4的資料,並取出最後的連號範圍 8 ,9 ,10
我是ROCK
rockchang@mails.fju.edu.tw