cihadakt
cihadakt

Reputation: 3214

time range and date range in TSQL

I want to write a procedure in TSQL that calculates peak times between given date ranges that splitted to time given.

Start time: 10-02-2012 10:00
End time  : 10-02 2012 11:00
time range: every 5 minutes

so it will be:

10:00 range 1  -> 5 peak times
10:05 range 2  -> 11 peak times
.
.
.
11:00 range 11 -> 7 peak times

when time range given 30 min then the code will calculate 2 ranges

Should I use Interval? How can i solve this problem? Any Help?

Upvotes: 1

Views: 1557

Answers (2)

Louis Ricci
Louis Ricci

Reputation: 21086

Use a loop with the DATEADD function.

http://msdn.microsoft.com/en-us/library/ms186819.aspx

Keep adding your "minute" inteval until the resulting date is larger than the end.

EDIT

BEGIN
-- setup
DECLARE @start DATETIME
DECLARE @end DATETIME
DECLARE @interval INT
DECLARE @samples TABLE (
    [time] DATETIME
)
SET @start = CAST('10-02-2012 10:00' as DATETIME)
SET @end = CAST('10-02-2012 11:00' as DATETIME)
SET @interval = 5
INSERT INTO @samples VALUES 
    ( '10-02-2012 9:00' ), ( '10-02-2012 10:00' ), ( '10-02-2012 10:02' )
    , ( '10-02-2012 10:02' ), ( '10-02-2012 10:05' ), ( '10-02-2012 10:20' )
    , ( '10-02-2012 10:34' ), ( '10-02-2012 11:30' )
-- make the ranges
DECLARE @ranges TABLE (
    [start] datetime
    ,[end] datetime
)
DECLARE @tmp DATETIME
SET @tmp = DATEADD(minute, @interval, @start)
IF @tmp > @end BEGIN SET @tmp = @end END
WHILE @start < @end
    BEGIN
    INSERT INTO @ranges VALUES (@start, @tmp)
    SET @start = @tmp
    SET @tmp = DATEADD(minute, @interval, @start)
    IF @tmp > @end BEGIN SET @tmp = @end END
    END
-- execute the query
SELECT r.[start], r.[end], count(s.[time]) [count]
FROM @ranges r
    LEFT JOIN @samples s 
        ON r.[start] <= s.[time] AND r.[end] > s.[time]
GROUP BY r.[start], r.[end]
END

I was suggesting using a simple while loop to generate the ranges you wanted. To me it's more straight-forward/easy to understand than the CTE recursive query solution, although admittedly less elegant.

Upvotes: 0

HABO
HABO

Reputation: 15816

You can determine the time ranges thusly:

declare @StartTime as DateTime = '10-02-2012 10:00'
declare @EndTime as DateTime ='10-02-2012 11:00'
declare @TimeRange as Time = '00:05:00.000'

; with TimeRanges as (
  select @StartTime as StartTime, @StartTime + @TimeRange as EndTime
  union all
  select StartTime + @TimeRange, EndTime + @TimeRange
    from TimeRanges
    where EndTime < @EndTime ) -- Corrected.
  select StartTime, EndTime
    from TimeRanges

Join the ranges with your sample data to obtain the summary:

declare @StartTime as DateTime = '10-02-2012 10:00'
declare @EndTime as DateTime ='10-02-2012 11:00'
declare @TimeRange as Time = '00:05:00.000'

declare @Samples as Table ( SampleId Int Identity, SampleTime DateTime )
insert into @Samples ( SampleTime ) values
  ( '10-02-2012 9:00' ), ( '10-02-2012 10:00' ), ( '10-02-2012 10:02' ), ( '10-02-2012 10:02' ),
  ( '10-02-2012 10:05' ), ( '10-02-2012 10:20' ), ( '10-02-2012 10:34' ), ( '10-02-2012 11:30' )

; with TimeRanges as (
  select @StartTime as StartTime, @StartTime + @TimeRange as EndTime
  union all
  select StartTime + @TimeRange, EndTime + @TimeRange
    from TimeRanges
    where EndTime < @EndTime ) -- Corrected.
  select StartTime, EndTime, Count( S.SampleId ) as Samples
    from TimeRanges as TR left outer join
      @Samples as S on TR.StartTime <= S.SampleTime and S.SampleTime < TR.EndTime
    group by TR.StartTime, TR.EndTime

Upvotes: 1

Related Questions