计算两次进入之间的失效时间

时间:2016-08-21 07:00:59

标签: sql

我有记录员工工作日志的表格。表列为staffid,orderid,Startingtime,endtime等。我知道如何找到两个时间之间的差异,即开始和结束时间。但是我需要知道让工人开始另一项工作需要多少分钟。 staffid workid开始时间结束时间 100 10222 11:30 pm 11.50 pm 20 100 20122 11.55 pm 01.55 pm 120 在这里,我需要计算两个工作之间的工作人员失去了多少分钟。(这里5分钟11.50 pm和11.55 pm)

2 个答案:

答案 0 :(得分:0)

使用以下查询查询获得所需结果。 注意:该查询将每天针对所有员工工作。

SELECT StaffiD,DATEDIFF(MINUTE,MIN(Startingtime),MAX(endingtime))-SUM(DATEDIFF(MINUTE,Startingtime,endingtime)) LapsedTime
FROM dbo.CalcTimeDiff
GROUP BY StaffiD,convert(date,Startingtime)
GO

以下查询将为您提供以分钟为单位的失效时间。

Java EE

答案 1 :(得分:0)

这里worklog是表的名称而不是'11 .50 AM',我将结果存储为'11:50 AM'。我想它解决了你的问题

DECLARE @DIFFERENCE INT,@TIME1 VARCHAR(10),@TIME2 VARCHAR(10),@ID INT
DECLARE @TEMP_DATE TABLE (ID INT IDENTITY(1,1),STAFFID INT, WORKID INT, 
STARTINGTIME VARCHAR(10), ENDINGTIME VARCHAR(10), TIMETAKEN INT)
INSERT INTO @TEMP_DATE SELECT * FROM WORKLOG

DECLARE C CURSOR FOR
SELECT ID FROM @TEMP_DATE
OPEN C
FETCH NEXT FROM C INTO @ID
WHILE @@FETCH_STATUS=0
BEGIN
IF (@ID != (SELECT COUNT(1) FROM WORKLOG))
BEGIN

SET @TIME1=(SELECT STARTINGTIME FROM @TEMP_DATE WHERE ID=@ID)
SET @TIME2=(SELECT STARTINGTIME FROM @TEMP_DATE WHERE ID=(@ID+1))
set @DIFFERENCE=(SELECT DATEDIFF(MINUTE,cast(@TIME1 as time), cast(@TIME2 as time)))
set @TIME1=(select cast(WORKID as varchar(20)) from @TEMP_DATE where id=@id)
set @TIME2=(select cast(WORKID as varchar(20)) from @TEMP_DATE where id=(@id+1))
PRINT 'Time difference between workid ('+@TIME1+' and '+@TIME2+') is    =>  '+cast(@DIFFERENCE as varchar(20))
END
FETCH NEXT FROM C INTO @ID
END
CLOSE C
DEALLOCATE C