Sql 如何从一系列日期中获得工作时间的总和?
我有trfdespacio表,其中登记了操作员编号、出发日期和到达日期 我有trfdespacho表,其中登记了操作员编号、出发日期和到达日期。 我想在出发日期和到达日期之间以小时和分钟为单位计算工作时间,以便以后获得休息时间。 例如,注册日期为3月10日1:14,结束日期为3月10日当天9:25,劳动时间为8小时11分钟。 用连续的劳动时间来获得我1天的休息时间, 如果我被要求报告3月10日至3月11日的日期,我该怎么做 因为我可以得到这两天的工作时间和休息时间 我有以下几点,我只能得到我一天工作的总时间,因为我可以做更多的天Sql 如何从一系列日期中获得工作时间的总和?,sql,sql-server,Sql,Sql Server,我有trfdespacio表,其中登记了操作员编号、出发日期和到达日期 我有trfdespacho表,其中登记了操作员编号、出发日期和到达日期。 我想在出发日期和到达日期之间以小时和分钟为单位计算工作时间,以便以后获得休息时间。 例如,注册日期为3月10日1:14,结束日期为3月10日当天9:25,劳动时间为8小时11分钟。 用连续的劳动时间来获得我1天的休息时间, 如果我被要求报告3月10日至3月11日的日期,我该怎么做 因为我可以得到这两天的工作时间和休息时间 我有以下几点,我只能得到我一天
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
您可以使用类似的方法来计算工作时间和非工作时间,然后使用您已经拥有的总和,并将其转换为所需的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
注意:您也可以计算窗口的总持续时间,只需减去工作时间即可得到非工作时间。这可能会对您有所帮助
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
你需要像3月10日、3月11日等每天的劳动时间和休息时间吗?或者您需要查询筛选器中给定的开始日期和结束日期之间的连续工时和休息时间?第二个问题我需要查询筛选器中给定的开始日期和结束日期之间的连续工时和休息时间