Home>

In fact, everyone slightly moves their brains,The problem can be transformed into,Is to find the maximum value in the smallest continuous array,The array size can be 1.

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

Do a full set of play,Send Buddha to the West.

To facilitate learning and research,Must write a full set of sample code.

-------------------------------------------------- -----------------------------------
 --by wls
 -Non-professional SQL does not seek efficiency, but seeks to run
 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 starting value
 declare @testscale integer
 set @[email protected] --data scale
 declare @t datetime,     @t datetime,     @dd int,     @dayadd int,     @tres datetime
 set @t="-::"
 set @t="-::"
 set @dd=datediff (dd, @t, @t)
 while @i<@testscale-data scale
   begin
    set @[email protected] * rand ()
    set @tres=dateadd (dd, @dayadd, @t) + rand ()
    insert into t_maxinmincontinuousarr values ​​(@i, @tres)
    set @[email protected] +
   end
 go
 --select top * from t_maxinmincontinuousarr
 --go
 -------------------------------------------------- -----------------------------------
 --delete some snid randomly
 declare @testscale integer
 [email protected]=--data scale
 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

Attach an execution plan

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

I don't know if this code can be used,Published the follow-up slowly.

Risky network code

Execute these two statements to clear the cache.

dbcc freeproccache
go
dbcc dropcleanbuffers
go

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

20151103-01

There is a problem with the code

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

20151103-02

I tried again (about dozens of guesses), and found that I can only handle missing searches within 2048.This is a sensitive number,Have to study.

Of course, it may be that I am not professional,There is a problem with the code written.

Fortunately, it is not a problem I encountered in development and production.You can also analyze and find problems in a leisurely way.

The revelation of this incident is:you little annoying reptile websites,The error code is here. I just don't change it.

Your behavior is illegal,This is not to say that notifications and deletions are fine.

I reserve all rights granted to me by law.

  • Previous smarty simple application examples
  • Next Detailed regular expression (RegExp)