2016-09-06 113 views
0

我想在下面的数据得到帮助,我们有手冲床节省出入境记录,各出入境记录可以存储不同行的雇员ID出勤时间和超时SQL查询

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

基本上每两个入口和出。

我想获得的时间和时间出去一天。正如你所看到的,这是一天中的时间和时间,但它存储了两条记录。 所以在这方面我很困惑如何解决这个问题。还要计算工时

一个帮助将是非常赞赏

+0

当时间不匹配会发生什么? –

+0

我只想获得特定员工当天的第一个和最后一个员工..时间不匹配..员工可以有多个出勤和多个入库 –

+0

所有我想要的第一个和最后一个出口...多数民众赞成在所有和在此基础上,我想计算的工作时间..我有点困惑现在..一点帮助将不胜感激..我是新手 –

回答

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 
+0

其返回重复的记录以及 –

+0

尝试用不同的条款.. –

+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 
+0

它不会按预期返回结果..它应该给出确切的时间和在同一天的时间,但它doesnot –

+0

你是什么意思? –

+0

在关键字'WITH'附近获得错误的语法 –