简体   繁体   English

Microsoft Access中的出勤查询

[英]Attendances query in Microsoft Access

this is my first time posting a question on this forum. 这是我第一次在此论坛上发布问题。 I have been struggling with writing a query in Microsoft Access for a week now and I hope someone here can help me out. 我一直在努力用Microsoft Access编写查询已有一周了,我希望这里有人可以帮助我。 I am building a time attendance application using fingerprint in vb6. 我正在使用vb6中的指纹构建考勤应用程序。

The table looks like this: 该表如下所示:

https://i.stack.imgur.com/MZcwI.png https://i.stack.imgur.com/MZcwI.png

As you can see in the table an employee can check in and out more than 2 times a day. 您可以在表格中看到,员工每天可以签入和签出2次以上。 My question is: How can i determine in the OriginType column which row is IN or Out? 我的问题是:如何确定OriginType列中的哪一行是IN或Out? When a employee checks in for the first time the OriginType should be "I". 员工首次签到时,OriginType应该为“ I”。 When he checks in for the second time the OriginType should be "O". 当他第二次签入时,OriginType应该为“ O”。 When he checks in for the 3th time the OriginType should be "I" again and so on. 当他第三次签入时,OriginType应该再次为“ I”,依此类推。

2nd question which is different from the last one. 第二个问题与上一个不同。

I want to write a query that selects from the timeInOut column. 我想编写一个从timeInOut列中选择的查询。 I would like the table to look like this: 我希望表格看起来像这样:

https://i.stack.imgur.com/GgAhx.png https://i.stack.imgur.com/GgAhx.png

As you can see there are 2 new columns now and there's no OriginType column anymore. 如您所见,现在有2个新列,并且不再有OriginType列。 I still want to use the correlated subquery and the modulus operator. 我仍然想使用相关子查询和模运算符。 When it's a checkin i want it to be placed in the column "CheckIn" and if it's a checkout i want it to be placed in the column "CheckOut". 当它是签入时,我希望将其放置在“ CheckIn”列中;如果它是签出,我希望将其放置在“ CheckOut”列中。

您可以从以下查询中获取最后一个OriginType,如果最后一个为“ O”,则插入“ I”;如果查询返回的行不存在,则插入“ I”;如果最后一个为“ I”,则不插入“ O”。

SELECT OriginType from Employee where employeeId = 1 and timeInOut = (select max(timeInOut) from Employee where employeeId = 1)

You can use a correlated subquery and the modulus operator for this: 您可以为此使用相关子查询和模运算符:

SELECT EmployeeID, 
    timeInOut, 
    IIF(
        (SELECT COUNT(*) 
        FROM MyTable s 
        WHERE s.EmployeeID = m.EmployeeID 
        AND s.timeInOut <= m.timeInOut
        AND s.timeInOut >= INT(m.timeInOut)) Mod 2 = 1, "I", "O") As OriginType
FROM MyTable m

This query works in the following way: 该查询以以下方式工作:

The subquery gets the amount of rows for that employee that have been posted on the same date as the current row. 子查询获取与当前行同一日期过帐的该员工行数。 Then, we calculate the modulus of 2 of that count, returning 1 if the count is not divisible by 2 (eg the 1st, 3rd, 5th etc check-in), and 0 if it's not. 然后,我们计算该计数的2的模数,如果计数不能被2整除(例如1、3、5等签入),则返回1,否则将返回0。

If the count is divisible by 2, then it must be a check in, if it's not, it's a check out. 如果计数可被2整除,则它必须是签入,如果不是,则是签出。

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

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