求教,sql如何判断数字的连续,并且获取断点的数字。
答案:3 悬赏:20 手机版
解决时间 2021-12-02 21:24
- 提问者网友:欲望失宠
- 2021-12-01 20:49
求教,sql如何判断数字的连续,并且获取断点的数字。
最佳答案
- 五星知识达人网友:行雁书
- 2021-12-01 21:17
设表名为TABLENAME,字段名为FIELD1则:
select FIELDN=FIELD1 FROM TABLENAME WHERe NOT EXISTS (SELECt FIELD1 FROM TABLENAME A WHERe A.FIELD1=tablename.field1+1)
union
select FIELDN=FIELD1 FROM TABLENAME WHERe NOT EXISTS (SELECt FIELD1 FROM TABLENAME A WHERe A.FIELD1+1=tablename.field1)
ORDER BY FIELDN
select FIELDN=FIELD1 FROM TABLENAME WHERe NOT EXISTS (SELECt FIELD1 FROM TABLENAME A WHERe A.FIELD1=tablename.field1+1)
union
select FIELDN=FIELD1 FROM TABLENAME WHERe NOT EXISTS (SELECt FIELD1 FROM TABLENAME A WHERe A.FIELD1+1=tablename.field1)
ORDER BY FIELDN
全部回答
- 1楼网友:愁杀梦里人
- 2021-12-01 22:56
-- get the min value, max value
declare @min_value int, @max_value int
select
@min_value = cast(min(focue) as int) ,
@max_value = cast(max(focue) as int)
from (select right(cast(thefield as varchar),5) as focue from thetable) as a
-- make serial table
declare @serials table (
idx int identity(100000,1),
focue char(5)
)
insert into @serials
select top 100000 ''
from sysobjects a, sysobjects b
delete from @serials
where 1=2
or idx < 100000+@min_value
or idx > 100000+@max_value
update @serials set focue = right(cast(idx as varchar),5)
-- list all lost
select focue
from @serials a
where not exists (select 1 from thetable b
where a.focue = right(cast(b.thefield as varchar),5)
)
- 2楼网友:蕴藏春秋
- 2021-12-01 22:10
加入表是test,列名nums,可以先做如下处理:
select T1.nums as nums,
case (select COUNT(1) from TEST T2 WHERe T2.nums like '%'+convert(varchar(10),convert(int,SUBSTRINg(T1.nums,len(T1.nums)-4,5))-1)) when 1 then (select T2.nums from TEST T2 WHERe T2.nums like '%'+convert(varchar(10),convert(int,SUBSTRINg(T1.nums,len(T1.nums)-4,5))-1)) ELSE NULL END AS has_parent,
ROW_NUMBER() OVER(order by nums asc) as row_num
from TEST T1
得到一个关系数据,对数据做分组分析就Ok拉。
select T1.nums as nums,
case (select COUNT(1) from TEST T2 WHERe T2.nums like '%'+convert(varchar(10),convert(int,SUBSTRINg(T1.nums,len(T1.nums)-4,5))-1)) when 1 then (select T2.nums from TEST T2 WHERe T2.nums like '%'+convert(varchar(10),convert(int,SUBSTRINg(T1.nums,len(T1.nums)-4,5))-1)) ELSE NULL END AS has_parent,
ROW_NUMBER() OVER(order by nums asc) as row_num
from TEST T1
得到一个关系数据,对数据做分组分析就Ok拉。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯