简体   繁体   中英

Calculating minutes within a defined time window?

I am looking to calculate the minutes spent performing a task that is within a specific time window.

I have the two input parameters defining the time window @WindowStart and @WindowEnd and i have [Task Start] and [Task End] columns in my task table.

What would be the easiest way to calculate the time spent on a task that is within my time window? Tasks can easily begin or end after @WindowStart and @WindowEnd .

If what you want is to find the length of tasks are entirely contained in the window:

select [task end] - [task start] as taskDuration
from tasktable
where [task start] > @WindowStart and [task end] < @WindowEnd

If instead you want to look at durations that have any overlap with the window, just flip the and to an or.

This might work? If the task start / end times are outside of the window start / end times, the difference would be negative. There are a few scenarios:

select TestDates.*,
 DATEDIFF(minute, 
   CASE WHEN TestDates.WindowStart > TestDates.TaskStart 
        THEN TestDates.WindowStart ELSE TestDates.TaskStart END, 
   CASE WHEN TestDates.WindowEnd < TestDates.TaskEnd
        THEN TestDates.WindowEnd ELSE TestDates.TaskEnd END
         ) as Interval
 from
(
SELECT GETDATE() as WindowStart,
dateadd(minute, 10, GETDATE()) as WindowEnd,
dateadd(minute, 2, GETDATE()) as TaskStart,
dateadd(minute, 14, GETDATE()) as TaskEnd
) as TestDates


select TestDates.*,
 DATEDIFF(minute, 
   CASE WHEN TestDates.WindowStart > TestDates.TaskStart 
        THEN TestDates.WindowStart ELSE TestDates.TaskStart END, 
   CASE WHEN TestDates.WindowEnd < TestDates.TaskEnd
        THEN TestDates.WindowEnd ELSE TestDates.TaskEnd END
         ) as Interval
 from
(
SELECT GETDATE() as WindowStart,
dateadd(minute, 10, GETDATE()) as WindowEnd,
dateadd(minute, -2, GETDATE()) as TaskStart,
dateadd(minute, 20, GETDATE()) as TaskEnd
) as TestDates

select TestDates.*,
 DATEDIFF(minute, 
   CASE WHEN TestDates.WindowStart > TestDates.TaskStart 
        THEN TestDates.WindowStart ELSE TestDates.TaskStart END, 
   CASE WHEN TestDates.WindowEnd < TestDates.TaskEnd
        THEN TestDates.WindowEnd ELSE TestDates.TaskEnd END
         ) as Interval
 from
(
SELECT GETDATE() as WindowStart,
dateadd(minute, 10, GETDATE()) as WindowEnd,
dateadd(minute, 20, GETDATE()) as TaskStart,
dateadd(minute, 30, GETDATE()) as TaskEnd
) as TestDates

select TestDates.*,
 DATEDIFF(minute, 
   CASE WHEN TestDates.WindowStart > TestDates.TaskStart 
        THEN TestDates.WindowStart ELSE TestDates.TaskStart END, 
   CASE WHEN TestDates.WindowEnd < TestDates.TaskEnd
        THEN TestDates.WindowEnd ELSE TestDates.TaskEnd END
         ) as Interval
 from
(
SELECT GETDATE() as WindowStart,
dateadd(minute, 10, GETDATE()) as WindowEnd,
dateadd(minute, -20, GETDATE()) as TaskStart,
dateadd(minute, -10, GETDATE()) as TaskEnd
) as TestDates

Simply shift the start / end of the task to the appropriate limits. Mind that this requires that start-time comes before end-time, if any of the pairs is reversed you'll need to shift them around first or you'll get strange results. Then again, when ever is end before start in the real world ? =)

DECLARE @task_start   datetime,
        @task_end     datetime,
        @window_start datetime,
        @window_end   datetime

SELECT @window_start = '09:00',
       @window_end   = '17:00',
       @task_start = '11:15',
       @task_end   = '19:22'

SELECT time_spent = (CASE WHEN @task_start > @window_end   THEN 0
                          WHEN @task_end   < @window_start THEN 0
                          ELSE DateDiff(minute, (CASE WHEN @task_start < @window_start THEN @window_start
                                                                                       ELSE @task_start END),
                                                (CASE WHEN @task_end > @window_end THEN @window_end
                                                                                   ELSE @task_end END))
                              END)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM