在定义的时间窗口内计算分钟数?

时间:2011-12-16 14:59:53

标签: sql sql-server-2005

我希望计算执行特定时间窗口内任务所花费的时间。

我有两个输入参数定义时间窗口@WindowStart@WindowEnd,我的任务表中有[任务开始]和[任务结束]列。

计算在我的时间窗口内的任务所花费的时间最简单的方法是什么?任务可以在@WindowStart@WindowEnd之后轻松开始或结束。

3 个答案:

答案 0 :(得分:1)

如果你想要的是找到任务的长度完全包含在窗口中:

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

如果您想要查看与窗口有任何重叠的持续时间,只需将其翻转到或。

答案 1 :(得分:0)

这可能有用吗?如果任务开始/结束时间超出窗口开始/结束时间,则差异将为负。有几种情况:

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

答案 2 :(得分:0)

只需将任务的开始/结束移至适当的限制即可。 请注意,这要求开始时间在结束时间之前到来,如果任何一对被逆转,你需要先将它们转移,否则你会得到奇怪的结果。那么,什么时候才能在真实世界开始之前结束? =)

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)