当前位置: 代码网 > it编程>数据库>MsSqlserver > SQL Server 2008 R2——查找最小nIndex,nIndex存在而nIndex+1不存在 求最小连续数组中的最大值

SQL Server 2008 R2——查找最小nIndex,nIndex存在而nIndex+1不存在 求最小连续数组中的最大值

2024年06月02日 MsSqlserver 我要评论
其实大家稍微动下大脑,问题可以转化为,是求最小连续数组中的最大值,数组大小可以为1。==========================================================

其实大家稍微动下大脑,问题可以转化为,是求最小连续数组中的最大值,数组大小可以为1。

=======================================================================

做戏做全套,送佛送到西。

为了便于学习研究,必然是要写全套示例代码的。

-------------------------------------------------------------------------------------
 --by wls
 --非专业sql 不求高效 但求能跑
 use tempdb
 go
 -------------------------------------------------------------------------------------
 if object_id (n't_maxinmincontinuousarr', n'u') is not null
 drop table t_maxinmincontinuousarr;
 go
 create table t_maxinmincontinuousarr(snid integer primary key,somedate datetime)
 go
 -------------------------------------------------------------------------------------
 declare  @i int
 set @i =       --snid起始值
 declare @testscale integer
 set @testscale=+@i  --数据规模
 declare @t datetime ,
     @t datetime ,
     @dd int ,
     @dayadd int ,
     @tres datetime
 set @t = '-- ::'
 set @t = '-- ::'
 set @dd = datediff(dd, @t, @t)
 while @i < @testscale  --数据规模
   begin 
    set @dayadd = @dd * rand() 
    set @tres = dateadd(dd, @dayadd, @t) + rand()  
    insert  into t_maxinmincontinuousarr values(@i , @tres)
    set @i = @i + 
   end
 go
 --select top * from t_maxinmincontinuousarr
 --go
 -------------------------------------------------------------------------------------
 --delete some snid randomly
 declare @testscale integer
 set @testscale=  --数据规模
 delete from t_maxinmincontinuousarr where snid=--(select abs(checksum(newid()))%@testscale + )
 delete from t_maxinmincontinuousarr where snid=--(select abs(checksum(newid()))%@testscale + )
 go
 --select top * from t_maxinmincontinuousarr
 --go
 -------------------------------------------------------------------------------------
 --now find the snid that snid+ is missing.
 with tminandmaxsnid
 as(
 select min(snid) as minsnid,max(snid) as maxsnid from t_maxinmincontinuousarr  --the min and max snid
 ),
 tcontinuousid
 as
 (
 select number as snidcmped from master..spt_values,tminandmaxsnid where type='p' and number >=tminandmaxsnid.minsnid and number <=tminandmaxsnid.maxsnid
 )
 select min(res.snidcmped)- from
 (
 select  snidcmped from tcontinuousid
 except 
 select  snid from t_maxinmincontinuousarr) as res 
 go

  附上执行计划

 =======================================================================

我也不知道这代码能不能用,先发表了后续慢慢改吧。

网络代码有风险 复制粘贴需谨慎

执行这两个语句清缓存。

dbcc freeproccache 
go
dbcc dropcleanbuffers
go

=======================================================================

20151103-01

代码有问题 有空改

=======================================================================

20151103-02

又尝试了一下(大概几十次猜范围),发现只能处理2048以内的缺失查找。这是个敏感的数字,得研究下。

当然也可能是我不专业,写的代码有问题。

幸好不是我在开发、生产中遇到的问题,还能悠哉悠哉的分析查找问题。

这件事的启示是:你们这些讨人厌的爬虫小网站,错误代码就在这里我还就是不改了。

        你们的行为是违法的,并不是说通知然后删除就是可以的。

        我保留一切法律赋予我的权利。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com