夜班员工使用lenel onguard的时间

时间:2019-08-27 09:38:59

标签: sql sql-server

我正在使用带有SQL Server dBase的Lenel Onguard来为我们的员工制作考勤系统。我总结了每一天的交易,以使他们成为第一笔交易,最后一天的退出,并获得datediff。得到持续时间。但是问题出在夜班员工身上,它显示了在同一天的早晨发生的超时,而第二天的实际退出时间是。所以datediff。返回错误的值。任何解决方案都受到欢迎!

下面的这段代码为我提供了错误的夜班人员退班时间,但正确的入场时间,并且该代码在白天班级的职员中也能很好地工作。根据我的代码,我有几个表,但是这里最重要的表是事件表:事件表具有称为devid的列,这是这里的外键和读者表上的主键。工作人员将事件表中的阅读器19或分区19作为进入的外键进行

我尝试了以下代码:

SELECT DISTINCT 
    BADGE.ID, 
    UPPER(ISNULL(dbo.EMP.FIRSTNAME, ' ') + ' ' + ISNULL(dbo.EMP.LASTNAME, ' ') + ' ' + ISNULL(dbo.EMP.MIDNAME, ' '))AS NAMES, 
    A.*,
    B.TIMEOUT, 
    datediff(hour,a.[TIMEIN],b.TIMEOUT) HoursWorked 
FROM (
    SELECT empid,convert(date,event_time_utc)[Date],ltrim(right(convert(varchar(25), DATEADD(HOUR,3,CAST(min(event_time_utc)AS TIME)), 100), 7)) TIMEIN 
    FROM events INNER JOIN READER ON EVENTS.DEVID=READER.READERID INNER JOIN EVENT ON EVENTS.EVENTTYPE=EVENT.EVTYPEID AND EVENTS.EVENTID=EVENT.EVID
    WHERE  READERID=19 AND PANELID=16 AND EVDESCR='Access Granted' 
    GROUP BY empid,convert(date,event_time_utc)
) A 
JOIN 
(
    SELECT empid,convert(date,event_time_utc)[Date],ltrim(right(convert(varchar(25), DATEADD(HOUR,3,CAST(MAX(event_time_utc)AS TIME)), 100), 7)) TIMEOUT
    FROM events INNER JOIN READER ON EVENTS.DEVID=READER.READERID INNER JOIN EVENT ON EVENTS.EVENTTYPE=EVENT.EVTYPEID AND EVENTS.EVENTID=EVENT.EVID
    WHERE READERID=20 AND PANELID=16 AND EVDESCR='Access Granted' 
    GROUP BY empid,convert(date,event_time_utc)
) B on A.empid=b.empid and a.[Date]=b.[Date]
JOIN Emp on emp.id=A.EmpID 
JOIN BADGE ON BADGE.EMPID=A.EMPID
ORDER BY DATE

这是我的预期结果

EmpID        TIMEIN                    Timeout
1       2014-08-21 21:38:06.000    2014-08-22 06:00:10.000                  
2       2014-08-22 22:30:00.000    2014-08-23 06:00:10.000

下面是事件表的结构

SERIALNUM DEVID EVENTID EMPID   EVENT_TIME_UTC
1513850367  19  77      1           08/21/14 21:38:06.000
1513850372  20  8       1           08/22/14 06:00:10.000
1513850374  19  199     1           08/22/14 10:01:15.000
1513850375  20  2       1           08/22/14 10:11:19.000
1513850376  19  2       2           08/21/14 22:01:26.000
1513850377  20  54      2           08/22/14 07:01:09.000
1513850381  19  10      2           08/22/14 10:03:12.000
1513850394  20  8       2           08/22/14 10:18:17.000
1513850396  19  199     2           08/22/14 11:18:23:000

1 个答案:

答案 0 :(得分:0)

您似乎只想将lead()min()作为窗口函数:

select empid, event_time_utc as timein, timeout
from (select e.*,
             min(case when e.devid <> 19 then e.event_time end) over
                 (partition by e.empid order by event_time_utc desc) as timeout
      from events e
     ) e
where devid = 19;