create table t2(x int constraint pk_t2 primary key);
go
insert into t2(x) values(1),(2),(3),(5),(7),(8),(11),(12),(13);
go
--解决方法 1:
with cteA as(
select x ,(select min(x) from t2 as b where b.x>=a.x and not exists (select x from t2 as c where c.x=b.x+1)) as endNumber
from t2 as a)
select min(x) as StartNumber,endNumber from cteA
group by endNumber;
go
--解决方法 2:
with cteB as(
select x,x-rowNumber as diff from (select x,ROW_NUMBER() over(order by x) as rowNumber from t2) as a)
select min(x),max(x) from cteB
group by diff;
go