简体   繁体   English

MS Access SQL 查询出勤时间

[英]MS Access SQL Query for time In/Out attendance

HI, 你好,

I'm doing a project which involves time and attendance management.我正在做一个涉及时间和出勤管理的项目。 When I download data from the biometric reader, I got the records in the following format,当我从生物识别阅读器下载数据时,我得到了以下格式的记录,

Lunch Break & Work Time Problem午休和工作时间问题

Attendance Log Table Name: dbo_CHECKINOUT The user Table name is: dbo_USERINFO考勤日志表名: dbo_CHECKINOUT用户表名: dbo_USERINFO

SELECT t.userid, dbo_USERINFO.NAME, DateValue(t.checktime) AS [date], 
Max(IIf(t.counter=0,t.checktime,Null)) AS Log_In, 
Max(IIf(t.counter=1,t.checktime)) AS LB_Out, 
Max(IIf(t.counter=2,t.checktime,Null)) AS LB_In, 
Max(IIf(t.counter=3,t.checktime)) AS Log_Out, 
Format((Log_In-LB_Out)+(LB_In-Log_Out),"hh:nn:ss") AS WorkTime, 
Format(LB_In-LB_Out,"hh:nn:ss") AS LunchBreak
FROM (
    SELECT t.*, 
      (select count(*) from dbo_CHECKINOUT where userid = t.userid and datevalue(checktime) = datevalue(t.checktime) and checktime < t.checktime) AS [counter] FROM dbo_CHECKINOUT AS t)  AS t INNER JOIN dbo_USERINFO ON t.USERID=dbo_USERINFO.USERID
GROUP BY t.userid, dbo_USERINFO.NAME, DateValue(t.checktime);

I want to show the above records as follows, (the Log_In, LB_Out, LB_In, Log_Out, WorkTime and LunchBreak are based on 'time') Someone from StackOverFlow helped me out to make this query我想将上述记录显示如下,(Log_In、LB_Out、LB_In、Log_Out、WorkTime 和 LunchBreak 基于“时间”) StackOverFlow 的某人帮我完成了这个查询

userid      date         Log_In        LB_Out        LB_In       Log_Out    WorkTime    LunchBreak
1           16-Feb-20   11:10:10 AM   7:51:57 PM                
1           17-Feb-20   11:28:17 AM                 
2           16-Feb-20   9:57:48 AM    1:58:19 PM    2:33:39 PM  9:29:29 PM  10:56:21     00:35:20
3           16-Feb-20   10:07:31 AM   1:45:15 PM    2:17:46 PM  9:29:20 PM  10:49:18     00:32:31
3           17-Feb-20   10:15:13 AM                 
4           16-Feb-20   10:36:01 AM   9:29:46 PM                
4           17-Feb-20   10:28:54 AM                 
5           16-Feb-20   9:33:08 AM    1:57:46 PM    2:48:26 PM  9:29:25 PM  11:05:37     00:50:40
5           17-Feb-20   9:31:47 AM  

Results:结果:

 userid date Log_In LB_Out LB_In Log_Out WorkTime LunchBreak 1 16-Feb-20 11:10:10 AM 7:51:57 PM 1 17-Feb-20 11:28:17 AM 2 16-Feb-20 9:57:48 AM 1:58:19 PM 2:33:39 PM 9:29:29 PM 10:56:21 00:35:20 3 16-Feb-20 10:07:31 AM 1:45:15 PM 2:17:46 PM 9:29:20 PM 10:49:18 00:32:31 3 17-Feb-20 10:15:13 AM 4 16-Feb-20 10:36:01 AM 9:29:46 PM 4 17-Feb-20 10:28:54 AM 5 16-Feb-20 9:33:08 AM 1:57:46 PM 2:48:26 PM 9:29:25 PM 11:05:37 00:50:40 5 17-Feb-20 9:31:47 AM

Now the problem is, as you can see Userid:1 & Userid: 4 doesn't have a LunchBreak.现在的问题是,正如您所看到的 Userid:1 & Userid: 4 没有 LunchBreak。 So their 1st log would be Log_In & 2nd Log would be as a Log_Out & total work time would be between 1st(Log_In) & 2nd(Log_Out).所以他们的第一个日志将是 Log_In & 2nd Log 将作为一个 Log_Out & 总工作时间将在 1st(Log_In) 和 2nd(Log_Out) 之间。

Please help to make this possible for me.请帮助我使这成为可能。

This fancy query will return the lunch breaks:这个奇特的查询将返回午休时间:

SELECT 
    dbo_UserInfo.UserId, 
    DateValue([CheckTime]) AS [Date], 
    TimeValue(Min([CheckTime])) AS LogIn, 

    (Select Max(TimeValue(T.CheckTime)) 
    From dbo_UserInfo As T 
    Where T.UserId = dbo_UserInfo.UserId 
    And DateValue(T.CheckTime) = DateValue(dbo_UserInfo.CheckTime) 
    And T.CheckTime >
        (Select Min(S.CheckTime) 
        From dbo_UserInfo As S 
        Where S.UserId = dbo_UserInfo.UserId 
        And DateValue(S.CheckTime) = DateValue(dbo_UserInfo.CheckTime))) As LogOut,

    (Select Min(TimeValue(T.CheckTime)) 
    From dbo_UserInfo As T 
    Where T.UserId = dbo_UserInfo.UserId 
    And DateValue(T.CheckTime) = DateValue(dbo_UserInfo.CheckTime)
    And T.CheckTime > 
        (Select Min(S.CheckTime) 
        From dbo_UserInfo As S 
        Where S.UserId = dbo_UserInfo.UserId 
        And DateValue(S.CheckTime) = DateValue(dbo_UserInfo.CheckTime))
    And T.CheckTime <
        (Select Max(S.CheckTime) 
        From dbo_UserInfo As S 
        Where S.UserId = dbo_UserInfo.UserId 
        And DateValue(S.CheckTime) = DateValue(dbo_UserInfo.CheckTime))
    Having Count(*) > 1) As LBIn,

    (Select Max(TimeValue(T.CheckTime)) 
    From dbo_UserInfo As T 
    Where T.UserId = dbo_UserInfo.UserId 
    And DateValue(T.CheckTime) = DateValue(dbo_UserInfo.CheckTime) 
    And T.CheckTime > 
        (Select Min(S.CheckTime) 
        From dbo_UserInfo As S 
        Where S.UserId = dbo_UserInfo.UserId 
        And DateValue(S.CheckTime) = DateValue(dbo_UserInfo.CheckTime))
    And T.CheckTime <
        (Select Max(S.CheckTime) 
        From dbo_UserInfo As S 
        Where S.UserId = dbo_UserInfo.UserId 
        And DateValue(S.CheckTime) = DateValue(dbo_UserInfo.CheckTime))
    Having Count(*) > 1) As LBOut
FROM 
    dbo_UserInfo
GROUP BY 
    dbo_UserInfo.UserId, 
    DateValue([CheckTime]);

From this you can easily calculate the work hours.由此您可以轻松计算工作时间。

在此处输入图片说明

Revised output:修改后的输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM