我只是在SQL Server中编写此函数。它计算工人的工作分钟数,不包括休息时间。
ALTER FUNCTION getMinutesWork
( @startT dateTime,
@endT dateTime)
AS
BEGIN
DECLARE @result int = DATEDIFF(mi, @startT, @endT) --time difference including break times
SET @startT = CAST(@startT AS TIME)
SET @endT = CAST(@endT AS TIME)
-- work schedule
DECLARE @t1 datetime = CAST('08:00:00' AS TIME), --work
@t2 datetime = CAST('10:00:00' AS TIME), --break
@t3 datetime = CAST('10:15:00' AS TIME), --w
@t4 datetime = CAST('12:40:00' AS TIME), --b
@t5 datetime = CAST('13:25:00' AS TIME), --w
@t6 datetime = CAST('16:30:00' AS TIME), --b
@t7 datetime = CAST('16:45:00' AS TIME), --w
@t8 datetime = CAST('18:15:00' AS TIME); --b
--excluding break times
IF ((@startT <= @t2 AND @endT >@t2) AND (@startT < @t3 AND @endT >= @t3))
SET @result = @result - DATEDIFF(mi, @t2, @t3)
IF ((@startT <= @t4 AND @endT >@t4) AND (@startT < @t5 AND @endT >= @t5))
SET @result = @result - DATEDIFF(mi, @t4, @t5)
IF ((@startT <= @t6 AND @endT >@t6) AND (@startT < @t7 AND @endT >= @t7))
SET @result = @result - DATEDIFF(mi, @t6, @t7)
IF ((@startT > @t8) AND (@endT > @t1))
SET @result = @result - DATEDIFF(mi, @t1, @startT)
--excluding break times if worker starts on break time
SET @result = (CASE
WHEN (@startT < @t1)
THEN @result - DATEDIFF(mi, @startT, @t1)
WHEN (@startT > @t2 AND @startT < @t3)
THEN @result - DATEDIFF(mi, @startT, @t3)
WHEN (@startT > @t4 AND @startT < @t5)
THEN @result - DATEDIFF(mi, @startT, @t5)
WHEN (@startT > @t6 AND @startT < @t7)
THEN @result - DATEDIFF(mi, @startT, @t7)
WHEN (@startT > @t8) AND (@endT < @t1)
THEN 0
ELSE @result
END)
----------'2004-10-19 20:00:00','2004-10-18 10:00:00'------------
--excluding break times if worker finish his work on break time
SET @result = (
CASE
WHEN (@endT < @t1)
THEN 0
WHEN (@endT >= @t2 AND @endT < @t3)
THEN @result - DATEDIFF(mi,@t2,@endT)
WHEN (@endT >= @t4 AND @endT < @t5)
THEN @result - DATEDIFF(mi,@t4,@endT)
WHEN (@endT >= @t6 AND @endT < @t7)
THEN @result - DATEDIFF(mi,@t6,@endT)
WHEN (@endT > @t8)
THEN @result - DATEDIFF(mi,@t8,@endT)
ELSE @result
END )
-----------------------------------------------------------------------------------
IF @result <0
SET @result = 0
-----------------------------------------------------------------------------------
RETURN @result
End
当工人在同一天来来往往时工作正常。但是当工人来到晚上11点工作到明天上午11点时,它并没有排除休息时间。有什么好主意吗?很抱歉很长一点。
编辑:我发现了问题,但无法解决问题。如果有@startT = '20:00:00'
和@endT = '10:20:00'
等过时工作,则此部分无法正常工作:
IF ((@startT <= @t2 AND @endT >@t2) AND (@startT < @t3 AND @endT >= @t3))
SET @result = @result - DATEDIFF(mi,@t2,@t3)
IF ((@startT <= @t4 AND @endT >@t4) AND (@startT < @t5 AND @endT >= @t5))
SET @result = @result - DATEDIFF(mi,@t4,@t5)
IF ((@startT <= @t6 AND @endT >@t6) AND (@startT < @t7 AND @endT >= @t7))
SET @result = @result - DATEDIFF(mi,@t6,@t7)
我该如何解决这个问题?
答案 0 :(得分:2)
请检查一下这是否有效? 我认为这解决了逻辑问题。
[[2,0,0,0],
[1,1,0,0],
[1,0,1,0],
…
[0,0,1,1],
[0,0,0,2]]
&#13;
答案 1 :(得分:0)
我编辑了if语句部分并且它有效:
--total work time if worker stay overnight including break times
IF ((@startT <= @t8) AND (@endT < @startT))
BEGIN
SET @result = DATEDIFF(mi,@startT,'23:59:59') + DATEDIFF(mi,'00:00:01',@endT) +1
SET @result = @result - DATEDIFF(mi,@t8,'23:59:59') - DATEDIFF(mi,'00:00:01',@t1)-1
END
-----------------------------------------------------------------------------------
--excluding break times
IF (((@startT > @t8) AND (@endT > @t1)))
SET @result = @result - DATEDIFF(mi,@startT,'23:59:59') - DATEDIFF(mi,'00:00:01',@t1)-1
IF ((@startT <= @t2 AND @endT >= @t3)) OR ((@startT > @t8) AND (@endT >= @t3))
SET @result = @result - DATEDIFF(mi,@t2,@t3)
IF ((@startT <= @t4 AND @endT >= @t5)) OR ((@startT > @t8) AND (@endT >= @t5))
SET @result = @result - DATEDIFF(mi,@t4,@t5)
IF ((@startT <= @t6 AND @endT >= @t7)) OR ((@startT > @t8) AND (@endT >= @t7))
SET @result = @result - DATEDIFF(mi,@t6,@t7)