这道题怎么做???

alanjue 2006-08-01 09:07:43
在SQL 数据库中有一字段的值为连续的且有断点的数字,
如1,2,3,4,5,6,7,8,9,10
12,13,14,15,
20,21,22,23,
100,101,102,103等,请问如何能通过查询找出各断点的最大和最小值?
...全文
123 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
billpu 2006-08-01
  • 打赏
  • 举报
回复
create table #1(
id int identity,
task varchar(5)
)

declare @max int
select @max=max(id) from table
select @max

declare @i int
set @i=1
while @i<=@max
begin
insert #1 select @i
set @i=@i+1
end

select max(id),min(id) from #1 where id not in (select id from table)
alanjue 2006-08-01
  • 打赏
  • 举报
回复

谢谢
liangpei2008 2006-08-01
  • 打赏
  • 举报
回复
或许借助临时表来比较断号更好一些!
liangpei2008 2006-08-01
  • 打赏
  • 举报
回复
Declare @t Table(F1 Int)
Insert @t Select 1
Union all Select 2
Union all Select 3
Union all Select 5
Union all Select 6
Union all Select 8
Union all Select 9
Union all Select 10
Union all Select 12
Union all Select 13
Union all Select 14
Union all Select 15
Union all Select 20
Union all Select 21
Union all Select 22
Union all Select 23
Union all Select 100
Union all Select 101
---
Select * From (
Select * From @t A
Where F1>1 And Not Exists
(Select 1 From @t Where F1=A.F1-1)
Union all
Select * From @t A
Where F1>1 And Not Exists
(Select 1 From @t Where F1=A.F1+1)) A
Order By F1
wuguang007 2006-08-01
  • 打赏
  • 举报
回复
如果是按降序或升序排列的
那就用left,right

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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