如何计算SQL Server中的夜班工作?

时间:2015-09-06 03:41:52

标签: sql-server fingerprint time-and-attendance

我使用指纹系统以4班制记录所有员工的IN OUT日志。正常班次(08:00-> 17:00),班次1:(06-> 14:00) ,班次2:(14:00-> 22:00)班次3:(明天22:00-> 06:00)。我有2个主表: enter image description here

enter image description here

当我使用左连接时:

select e.Id as EmpID,CAST(PunchTime as DATE)CheckDate,MIN(cast(a.PunchTime as Time))[TimeIN], max(cast(a.PunchTime as Time))[Time_OUT]
from Employee e
left join AttLog a
on a.EnrollNumber=e.EnrollNumber 
group by e.Id,CAST(PunchTime as DATE)

enter image description here

当我使用内连接时:

select e.Id as EmpID,CAST(PunchTime as DATE)CheckDate,MIN(cast(a.PunchTime as Time))[TimeIN], max(cast(a.PunchTime as Time))[Time_OUT]
from Employee e
inner join AttLog a
on a.EnrollNumber=e.EnrollNumber 
group by e.Id,CAST(PunchTime as DATE)

enter image description here

然后,您看到使用左连接时我们获得所有员工,包括空时间。 当我们使用内部联接时,如果员工在Shift 3工作(明天22:00到明天06:00),我们就会得到Time IN = Time OUT。 所以我的问题是如何计算Shift 3的IN和OUT时间。 如果员工只打了IN然后Time OUT = Time IN,那么在这种情况下如何将时间OUT显示为00:00:00。 我想这样输出:

EmpID  CheckDate   TimeIN                       Time_OUT
5      2015-08-19    2015-08-19 07:51:29.000      2015-08-20 07:43:57.000
14     2015-08-19    2015-08-19 06:52:26.000      2015-08-19 00:00:00.000

EmpID 5正常工作:08:00-> 17:00但他必须上夜班,所以他必须留在公司,直到明天08:00。 EmpID 14在正常班次工作,但她忘记了冲出去。 目前有上述数据,输出如下:

EmpID  CheckDate   TimeIN                       Time_OUT
    5      2015-08-19    2015-08-19 07:51:29.000      2015-08-19 07:51:29.000
    5      2015-08-20    2015-08-20 07:43:57.000      2015-08-20 07:43:57.000
    14     2015-08-19    2015-08-19 06:52:26.000       2015-08-19 06:52:26.000

2 个答案:

答案 0 :(得分:2)

您可以参考以下解决方案,经过大量研究后得到的解决方案。此解决方案仅适用于SQL 2012或更高版本。

;WITH Level1 
AS ( SELECT 
     EmpName (or EmpID) 
    ,TrnName (Exit or Entrance)
    ,EntryDateTime
    ,LAG (TrnName, 1, 'N/A') OVER ( PARTITION BY EmpName ORDER BY EntryDateTime) AS LastEvent
    ,LEAD(TrnName, 1, 'N/A') OVER ( PARTITION BY EmpName ORDER BY EventDateTime ) AS NextEvent 
    FROM #TempData 

    ),
Level2
AS ( SELECT 
     EmpName 
    ,TrnName 
    ,EntryDateTime 
    ,LastEvent
    ,NextEvent 
    FROM   Level1 
    WHERE
        NOT ( TrnName = 'Entrance' AND NextEvent = 'Entrance' )
        AND NOT ( TrnName = 'Entrance' AND LastEvent = 'Entrance' )
        AND NOT ( TrnName = 'Exit' AND LastEvent = 'Exit' )
        AND NOT ( TrnName = 'Entrance' AND NextEvent = 'N/A' ) 
        AND NOT (TrnName = 'Exit' AND LastEvent = 'N/A' )
    ),
Level3
AS ( SELECT 
     EmpName
    ,TrnName 
    ,EntryDateTime 
    ,DATEDIFF(second, EntryDateTime,LEAD(EntryDateTime) OVER ( PARTITION BY EmpName ORDER BY EntryDateTime )) AS Seconds 
    FROM Level2 
    )

SELECT 
     EmpName 
    ,EntryDateTime 
    ,(EntryDateTime+convert(DateTime,TIMEFROMPARTS((Seconds%(3600*24)/3600), ((Seconds%(3600*24)%3600)/60), ((Seconds%(3600*24)%3600)%60),0, 0))) AS ExitDateTime
    ,Seconds
    ,TIMEFROMPARTS((Seconds%(3600*24)/3600), ((Seconds%(3600*24)%3600)/60), ((Seconds%(3600*24)%3600)%60),0, 0) AS WorkTime
    FROM Level3 
    WHERE 
        TrnName = 'Entrance' 

这可能会有所帮助,可以通过添加 PeterDNCO 提供的阈值代码来改进。

答案 1 :(得分:1)

这是一种对打孔时间进行排序的方法,然后通过递归CTE自行连接以将时间拼接在一起。像这样的系统很难控制错过的拳击等,但我尝试通过添加一个小时工作阈值来向你展示一种方法。

/* Create some sample Employee punch data to test*/
IF OBJECT_ID('tempdb..#AttLogTest') IS NOT NULL
    DROP TABLE #AttLogTest

CREATE TABLE #AttLogTest (EnrollNumber INT NOT NULL, PunchTime DATETIME NOT NULL)

INSERT INTO #AttLogTest (EnrollNumber, PunchTime)
SELECT 10, '2015-08-01 08:01:03' UNION ALL
SELECT 10, '2015-08-02 07:57:35' UNION ALL
SELECT 10, '2015-08-01 16:15:23' UNION ALL
SELECT 10, '2015-08-02 16:17:46' UNION ALL
SELECT 12, '2015-08-01 21:59:31' UNION ALL
SELECT 12, '2015-08-02 05:59:02' UNION ALL
SELECT 12, '2015-08-02 22:02:28' UNION ALL
SELECT 12, '2015-08-03 06:01:24' UNION ALL
SELECT 14, '2015-08-01 07:59:01' UNION ALL
SELECT 14, '2015-08-02 07:58:16' UNION ALL
SELECT 14, '2015-08-02 16:02:48'

/* Employee time query logic below */

/* First, create a temp table that sequences the punch times for each employee */
IF OBJECT_ID('tempdb..#EmployeeTimeSequence') IS NOT NULL
    DROP TABLE #EmployeeTimeSequence

SELECT
    EnrollNumber
    ,PunchTime
    ,PunchSequence = ROW_NUMBER() OVER(PARTITION BY EnrollNumber ORDER BY PunchTime)
INTO #EmployeeTimeSequence
FROM #AttLogTest --Replace this with your dbo.AttLog table if this solution works for you
/*WHERE clause could be added here to filter for specific dates or EnrollNumbers */


/* If time between punches is greater than this threashold, then it will be treated as a missed punch
  in logic below. Remove this or modify as needed. */
DECLARE @MissedPunchThreshold int
SET @MissedPunchThreshold = 20

/* Next, create a recursive CTE which will stitch together the punch times and ensure punch times don't overlap when 
 self-joining to #EmployeeTimeSequence. */
;WITH EmployeeTimeCTE (EnrollNumber, CheckDate, Time_In, Time_Out, HoursBetweenPunch, PunchOutSequence)
AS (
    /* Anchor member */
    SELECT 
        ETS_In.EnrollNumber
        ,CAST(ETS_In.PunchTime AS DATE) AS CheckDate
        ,ETS_In.PunchTime AS Time_In
        ,ETS_Out.PunchTime AS Time_Out
        ,DateDiff(hour, ETS_In.PunchTime, ETS_Out.PunchTime) AS HoursBetweenPunch
        ,ETS_Out.PunchSequence AS PunchOutSequence
    FROM #EmployeeTimeSequence AS ETS_In
    LEFT OUTER JOIN #EmployeeTimeSequence AS ETS_Out
        ON ETS_In.EnrollNumber = ETS_Out.EnrollNumber
        AND ETS_Out.PunchSequence = ETS_In.PunchSequence + 1
    WHERE ETS_In.PunchSequence = 1

    UNION ALL
    /* Recursive memebr - build on top of anchor */
        SELECT 
            ETS_In.EnrollNumber
            ,CAST(ETS_In.PunchTime AS DATE) AS CheckDate
            ,ETS_In.PunchTime AS Time_In
            ,ETS_Out.PunchTime AS Time_Out
            ,DateDiff(hour, ETS_In.PunchTime, ETS_Out.PunchTime) AS HoursBetweenPunch
            ,ETS_Out.PunchSequence AS PunchOutSequence
    FROM #EmployeeTimeSequence AS ETS_In --get the time for the in punch
    INNER JOIN EmployeeTimeCTE ET
        ON ET.EnrollNumber = ETS_In.EnrollNumber
            AND ETS_In.PunchSequence =
                CASE
                    WHEN ET.HoursBetweenPunch > @MissedPunchThreshold -- if more than threshold, then treat as missed punch
                        THEN ET.PunchOutSequence -- then treat the previous out punch as the next in punch instead
                    ELSE ET.PunchOutSequence + 1  -- else join as usual to get the next punch in sequence
                END 
    INNER JOIN #EmployeeTimeSequence AS ETS_Out -- get the time for the out punch
        ON ETS_In.EnrollNumber = ETS_Out.EnrollNumber
        AND ETS_Out.PunchSequence = ETS_In.PunchSequence + 1
)
/* Now query the CTE */
SELECT 
    EnrollNumber AS EmpID
    ,CheckDate 
    ,Time_In
    ,CASE WHEN HoursBetweenPunch > @MissedPunchThreshold THEN NULL ELSE Time_Out END AS Time_Out
    ,CASE WHEN HoursBetweenPunch > @MissedPunchThreshold THEN NULL ELSE HoursBetweenPunch END AS HoursBetweenPunch
FROM EmployeeTimeCTE
ORDER BY EnrollNumber, CheckDate
OPTION (MAXRECURSION 1000)