SQL2005中怎样判断一组数字是否为连续数?

yyanzh 2011-06-14 11:25:32
想问问在一张表中有1到999999个数字,怎样可以找出这些连续数字中缺了那个数字?比如像1,2,3,4,5,8,9,11,15 这组连续数字中没有6,7,10,12,13,14,怎样查询出没有的数字:6,7,10,12,13,14
...全文
51 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yueaiwo 2011-06-15
  • 打赏
  • 举报
回复
scl
叶子 2011-06-14
  • 打赏
  • 举报
回复

declare @a table (id int)
insert into @a
select 1 union all
select 3 union all
select 5 union all
select 6 union all
select 7 union all
select 10 union all
select 12 union all
select 14 union all
select 16

select distinct b.number from @a
a right join master..spt_values b on a.id=b.number
where b.number between (select min(id) from @a)
and (select max(id) from @a) and a.id is null

/*
number
-----------
2
4
8
9
11
13
15
*/

X_0 2011-06-14
  • 打赏
  • 举报
回复

select distinct t1.number
from master..spt_values t1 left join tb t2 on t1.number=t2.id
where t1.number between (select MIN(id) from tb) and (select MAX(id) from tb)
and t2.id is null

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧