Suppose there are chronologically sorted records in a table:
Here dateVal column dictates chronology. Objective: for any specified N (integer) display all from-to groups of records that are more than N days apart. For N=7 expected results are (dates in US format):
I had one very similar problem a year ago (membership continuity analysis)... needless to say dude who made it before was while-not-eof happy and code speed was somewhere between 'continental drift' and ''.
Once you get it done, run this code (only once) to get... 4600+ sample rows and try again. Best exec time wins.
Although this problem is relatively simple in procedural languages, non-SQL folks are also welcomed to participate... say, shortest code wins.
------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
Code:
create table sampleData( dateVal smalldatetime primary key, someColumn varchar(32) )
insert into sampleData (dateVal)
select '19990218' union all
select '19990301' union all
select '19990302' union all
select '19990307' union all
select '19990315' union all
select '19990323' union all
select '19990330' union all
select '19990401' union all
select '19990408'
update sampleData
set someColumn = 'blah ' + convert(varchar(10), dateVal, 101)
Code:
dateFrom someColumnFrom dateTo someColumnTo
----------.----------------.-----------.----------------
02/18/1999 blah 02/18/1999 02/18/1999 blah 02/18/1999
03/01/1999 blah 03/01/1999 03/07/1999 blah 03/01/1999
03/15/1999 blah 03/15/1999 03/15/1999 blah 03/15/1999
03/23/1999 blah 03/23/1999 04/08/1999 blah 04/08/1999
I had one very similar problem a year ago (membership continuity analysis)... needless to say dude who made it before was while-not-eof happy and code speed was somewhere between 'continental drift' and ''.
Once you get it done, run this code (only once) to get... 4600+ sample rows and try again. Best exec time wins.
Code:
declare @dmin smalldatetime, @delta int
select @dmin = min(dateval), @delta = 8 + datediff(dd, min(dateval), max(dateval)) from sampleData
declare @t table( N int )
insert into @t values (1)
while @@rowcount <256 insert into @t select N+(select count(*) from @t) from @t
insert into sampleData (dateVal, someColumn)
select dateVal + @delta*N, 'blah ' + convert(varchar(10), dateVal + @delta*N, 101)
from sampleData cross join @t
Although this problem is relatively simple in procedural languages, non-SQL folks are also welcomed to participate... say, shortest code wins.
------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]