如何从一系列日期中获得工作小时数?

时间:2017-04-07 20:49:56

标签: sql sql-server

我有trfdespacio表,其中记录了运营商号码,出发日期和到达日期。

trfdespacho

我有trfdespacho表,其中注册了运营商号码,出发日期和到达日期。 我希望在出发日期和到达日期之间以小时和分钟计算时间,以便稍后获得我休息的时间。 例如,注册日期为3月10日1:14,并在3月10日至9:25结束,劳动时间为8小时,11分钟。 用连续的工作时间来获得我在1天休息的时间, 如果我被要求报告3月10日至3月11日的日期,我该怎么办? 因为我可以得到这两天工作的时间和我休息的时间。

我有以下内容,我只能在1天内完成工作总时间,因为我可以做更多天。

SELECT 
    D.numOperador,
(cast(SUM(DATEDIFF(MINUTE, D.FechaSalida, D.FechaLlegada)) / 60 as varchar)  + +':'+
cast( SUM(DATEDIFF(MINUTE, D.FechaSalida, D.FechaLlegada)) % 60 AS varchar )  ) as TiempoLaborado
    FROM 
    trkOperadores O
    INNER JOIN trfDespacho D ON O.NumOperador = D.numOperador
WHERE O.NumOperador = 900200 
    AND D.FechaSalida>='2017-03-10 00:00:00.000' AND D.FechaLlegada<=DATEADD(DAY, 1, '2017-03-10 00:00:00.000') 
GROUP BY 
    D.NumOperador, FechaSalida, FechaLlegada
GO

Resultado

2 个答案:

答案 0 :(得分:0)

你可以使用这样的东西来计算工作时间和非工作时间,然后使用已有的东西对它求和并将其转换为你想要的HH:MM格式。

只需创建测试数据......

drop table #test

create table #test (depart datetime, arrive datetime)
insert into #test values ('1-1-2017 10:00:00', '1-1-2017 10:15:00')
insert into #test values ('1-1-2017 11:00:00', '1-1-2017 11:25:00')
insert into #test values ('1-1-2017 11:25:00', '1-1-2017 11:56:00')
insert into #test values ('1-2-2017 13:05:00', '1-2-2017 15:00:00')
insert into #test values ('1-3-2017 13:05:00', '1-3-2017 15:00:00')
insert into #test values ('1-3-2017 20:00:00', '1-4-2017 1:00:00')
insert into #test values ('1-4-2017 13:05:00', '1-4-2017 15:00:00')

设置窗口:

declare @begin datetime = '1-1-2017 00:00:00'
declare @end datetime = '1-3-2017 23:59:59'

使用lead获取每次旅行的下一个出发时间。然后使用datediff计算出发和到达之间的时间(或窗口的开始和到达,如果出发在窗口开始之前)和窗口的到达或结束。

然后使用datediff计算到达和下一次出发之间的时间。

; with cte as (
    select *
        , lead(depart) over (order by depart, arrive) as NextDeparture 
    from #test
    where depart between @begin and @end 
        or arrive between @begin and @end)

select *
    , datediff(minute, case when depart < @begin then @begin else depart end, case when arrive > @end then @end else arrive end) as WorkTime
    , datediff(minute, case when arrive > @end then @end else arrive end, case when arrive > @end or nextdeparture is null then @end else nextdeparture end) as NonWorkTime
from cte

注意:您还可以计算窗口的总持续时间,只需减去工作时间即可获得非工作时间。

答案 1 :(得分:0)

这可能对您有帮助,

DECLARE
    @FirstDate datetime,
    @LastDate datetime

SELECT
    @FirstDate = '2017-03-11 00:00:00.000',
    @LastDate = '2017-03-14 00:00:00.000';

With A as (
Select '2017-03-10 09:00:00.000' as S, '2017-03-10 17:00:00.000' as E union 
Select '2017-03-11 09:00:00.000' as S, '2017-03-11 17:00:00.000' as E union 
Select '2017-03-12 09:00:00.000' as S, '2017-03-12 17:00:00.000' as E union 
Select '2017-03-13 09:00:00.000' as S, '2017-03-13 17:00:00.000' as E union 
Select '2017-03-14 09:00:00.000' as S, '2017-03-14 17:00:00.000' as E 
)

Select 
Sum(DATEDIFF(minute, S, E)) as [Worked Minutes], 
DATEDIFF(minute, @FirstDate, @LastDate) as [Total Duration in Minutes], 
DATEDIFF(minute, @FirstDate, @LastDate) - Sum(DATEDIFF(minute, S, E)) as [Rested Minutes]
-- You need to convert the rested minutes to hours and probably in days if it exeeds the 24 hours limit
from A 
Where S >= @FirstDate and E <= @LastDate