Reputation: 503
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
.
Upvotes: 0
Views: 170
Reputation: 6002
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)
Upvotes: 0
Reputation: 8726
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
Upvotes: 0
Reputation: 6361
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.
Upvotes: 1