Here is one way that assumes you have only the date column to determine sequence. set nocount ondeclare @tb table (dt datetime)insert @tbselect dateadd(day, 0, getdate()) union allselect dateadd(day, 5, getdate()) union allselect dateadd(day, 13, getdate()) union allselect dateadd(day, 15, getdate()) select dt ,intervalFromPrevious = datediff(day, (select max(dt) from @tb where dt < a.dt), dt)from @tb aorder by dt desc--I'm just using the same statement as a derived table to get the MaxIntervalselect max(interval) maxIntervalfrom ( select interval = datediff(day, (select max(dt) from @tb where dt < a.dt), dt) from @tb a ) aOUTPUT:dt intervalFromPrevious ------------------------------------------------------ -------------------- 2006-04-05 10:16:37.433 22006-04-03 10:16:37.433 82006-03-26 10:16:37.433 52006-03-21 10:16:37.433 NULLmaxInterval ----------- 8
Be One with the OptimizerTG