考勤时间和超时Sql查询

时间:2016-09-06 10:33:09

标签: sql sql-server sql-server-2008-r2

我想获得以下数据的帮助,我们有手动打孔机,可以保存退出和输入日志,每个退出和输入日志都为员工ID存储不同的行

EMP ID |  EntryTime                | ExitTime
11769     2015-02-01 08:00:00        NULL
11769     NULL                       2015-02-01 13:00:00

基本上每两个进入和进出。

我想抽出一天的时间和时间。正如您所看到的,这是一天的时间和时间,但它存储了两条记录。 所以在这方面我很困惑如何解决这个问题。也想计算工作时间

非常感谢帮助

3 个答案:

答案 0 :(得分:0)

如果您的结构完全在您提供的示例中,并且您没有背靠背滑动(例如,员工可能会刷卡两次,以为第一个未提交),那么您可以执行如下查询:

WITH InOut (empId, EntryTime, ExitTime) as
(SELECT  empId ,
        a1.EntryTime ,
        ( SELECT    MIN(a2.ExitTime)
          FROM      attendance a2
          WHERE     a1.empId = a2.empId
                    AND a1.EntryTime < a2.ExitTime
        )
FROM    attendance a1
WHERE   EntryTime IS NOT NULL
)
SELECT empId ,
       EntryTime ,
       ExitTime,
       DATEDIFF(Minute, EntryTime, ExitTime) AS minutes FROM InOut;

编辑: 我不明白你的评论。以下是完整示例:

DECLARE @attendance TABLE
    (
      EMPID INT ,
      EntryTime DATETIME NULL ,
      ExitTime DATETIME NULL
    );

INSERT  @attendance
        ( EMPID, EntryTime, ExitTime )
VALUES  ( 11769, '2015-02-01 08:00:00', NULL ),
        ( 11769, NULL, '2015-02-01 13:00:00' ),
        ( 11769, '2015-02-02 09:00:00', NULL ),
        ( 11769, NULL, '2015-02-02 12:00:00' ),
        ( 11769, '2015-02-03 08:00:00', NULL ),
        ( 11769, NULL, '2015-02-03 13:00:00' ),
        ( 1, '2015-02-01 08:10:00', NULL ),
        ( 1, NULL, '2015-02-01 13:10:00' ),
        ( 1, '2015-02-02 09:10:00', NULL ),
        ( 1, NULL, '2015-02-02 12:10:00' ),
        ( 1, '2015-02-03 08:10:00', NULL ),
        ( 1, NULL, '2015-02-03 13:10:00' ),
        ( 2, '2015-02-01 08:30:00', NULL ),
        ( 2, NULL, '2015-02-01 13:30:00' ),
        ( 2, '2015-02-02 09:30:00', NULL ),
        ( 2, NULL, '2015-02-02 12:30:00' ),
        ( 2, '2015-02-03 08:30:00', NULL );

WITH    InOut ( empId, EntryTime, ExitTime )
          AS ( SELECT   EMPID ,
                        a1.EntryTime ,
                        ( SELECT    MIN(a2.ExitTime)
                          FROM      @attendance a2
                          WHERE     a1.EMPID = a2.EMPID
                                    AND a1.EntryTime < a2.ExitTime
                        )
               FROM     @attendance a1
               WHERE    EntryTime IS NOT NULL
             )
    SELECT  empId ,
            EntryTime ,
            ExitTime ,
            DATEDIFF(MINUTE, EntryTime, ExitTime) AS minutes
    FROM    InOut;

结果是:

empId   EntryTime               ExitTime                minutes
11769   2015-02-01 08:00:00.000 2015-02-01 13:00:00.000 300
11769   2015-02-02 09:00:00.000 2015-02-02 12:00:00.000 180
11769   2015-02-03 08:00:00.000 2015-02-03 13:00:00.000 300
1       2015-02-01 08:10:00.000 2015-02-01 13:10:00.000 300
1       2015-02-02 09:10:00.000 2015-02-02 12:10:00.000 180
1       2015-02-03 08:10:00.000 2015-02-03 13:10:00.000 300
2       2015-02-01 08:30:00.000 2015-02-01 13:30:00.000 300
2       2015-02-02 09:30:00.000 2015-02-02 12:30:00.000 180
2       2015-02-03 08:30:00.000 NULL                    NULL

答案 1 :(得分:0)

在这种情况下使用子查询。

 SELECT at1.empId ,at1.EntryTime ,
        (SELECT    MIN(a2.ExitTime)
          FROM      attendance at2
          WHERE     at1.empId = at2.empId
                    AND  at2.ExitTime >at1.EntryTime ) ExitTime
FROM    attendance at1
WHERE   at1.EntryTime IS NOT NULL

答案 2 :(得分:0)

您可以使用Case以及MIN和MAX来获得先进先出。然后使用dateDiff函数获取小时/分钟数