VBA用于计算班次计算的差异

时间:2017-07-11 17:48:44

标签: sql vba excel-vba adodb excel

我正在尝试使用VBA / SQL基于EventType(In或Out)的滑动报告获取每位员工的工作时间。

到目前为止,我已经获得了总登录时间,但无法计算输入/退出拆分中间的中断。任何人都可以帮我指点正确的方向。

SELECT 
    StaffMember, Date, 
    min(EventDateTime) as Max_In, 
    max(EventDateTime) as Max_Out, 
    (max(EventDateTime)- min(EventDateTime))*24 as Whrs
From [data$] 
group by StaffMember, Date

Example Set

1 个答案:

答案 0 :(得分:1)

如果您的SQL风格支持窗口函数,您可以这样处理它:

SELECT Entrances.StaffMember,
       EnterTime,
       ExitTime,
       ExitTime - EnterTime As TimeBetween
  FROM (SELECT StaffMember,
               EventDateTime As EnterTime,
               ROW_NUMBER() OVER
                 ( PARTITION BY StaffMember
                       ORDER BY EventDateTime ASC
                 ) As EnterRnk
          FROM data
         WHERE EventType = 'Enter'
       ) AS Entrances
 INNER /*
        Or left join and coalesce to a high value
        if you don't want to drop anything
       */
  JOIN (SELECT StaffMember,
               EventDateTime As ExitTime,
               ROW_NUMBER() OVER
                 ( PARTITION BY StaffMember
                       ORDER BY EventDateTime ASC
                 ) As ExitRnk
          FROM data
         WHERE EventType = 'Exit'
       ) AS Exits
    ON Entrances.StaffMember = Exits.StaffMember
   AND Entrances.EnterRnk = Exits.ExitRnk

否则你必须看看它是否支持递归查询。