34,588
社区成员
发帖
与我相关
我的任务
分享
--不考虑起始号码断号的情况:
declare @t table(id int)
insert into @t select 1 union select 2 union select 5 union select 7
select
rtrim(a.ID)+case when min(b.ID)=a.ID then '' else '-'+rtrim(min(b.ID)) end 断号区间
from
(select ID+1 as ID from @t t where not exists(select 1 from @t where ID=t.ID+1)) a,
(select ID-1 as ID from @t t where not exists(select 1 from @t where ID=t.ID-1)) b
where
a.ID<=b.ID
group by
a.ID
/*
断号区间
-------------------------
3-4
6
*/