Hello everyone.
I'm new around here and I have a problem to which I'm not sure how to solve nor how to better entitle it.
I'm working on a company that has a time clock machine, where every employee is required to register their entries and exits through their fingerprints.
The employees are divided in several groups, where the most relevants ones to this case are the office and logistics employees.
The logistics operators are required to register their entry and exit for the lunch period as well and may not register in several occasions, such as working hours compensation, etc. This means that every logistics operator will have a maximum of 4 registrations per day (2 entries and 2 exits).
There are a few things to take into consideration:
- Sometimes the employees don't register their entries and/or exits (making everything very chaotic...)
- The lunch period isn't accounted into the current database tables (meaning, the Human Resources Department, pretty much has to check the entry and exit timestamps and calculate the difference for the lunch period...)
The result that is needed to be achieved is to calculate the working hours and lunch period hours of each employee in order to calculate the total workings hours per day, considering the days where the employees haven't shown up for work, no matter the reason.
The current database that holds significant information for this are 3:
- Userinfo (Information regarding the users registered on the time clock machine)
- Checkinout (Information regarding the entries and exits each employee)
- Dept (Information regarding to each department)
So, the logic is: every employee has its department and their respect entries and exits for each day.
A problem I found on the database (and I have no idea why this happens...). Since employees often forget to register their entries and/or exits, I tried to retrieve every record between the tables Userinfo and Checkinout, whether the employees have registered on not, meaning, I used a LEFT JOIN between the two tables, with the intention of retrieving null values for the Checkinout table on the days that the employees didn't register their fingerprint.
This isn't working, the records that are returned are exclusive only to the days where the employees register their fingerprint and this makes things even more complicated...
I'm using the following query to retrieve all the entries and exits of every employee between a range of days, prepared for excel connection:
This works fine and dandy as you can see on the below image:Code:SELECT P.CheckTime, P.CheckType, U.Name, D.DeptName FROM ((Checkinout P LEFT JOIN Userinfo U ON P.Userid = U.Userid) LEFT JOIN Dept D ON U.Deptid = D.Deptid) WHERE P.CheckTime BETWEEN ? AND (DATEADD('d',1,?)) ORDER BY U.Name
Subtitles:
I - Entry
O - Exit
Armz Escritório - Office Employees
Armz Plataforma - Logistics Employees
However, the table that is returned poses as real problem for what is to be achieved. Since we need to check the working hours for each day, we need to subtract the exit timestamp from the entry timestamp. This is not possible as each record returns the timestamp regarding if the record is an entry or exit.
To achieve the goal we'd need to have a query returning something like this:
This way it would already be possible to calculate the total working hours for each employee, but we'd still have the problem for the lunch period, because for the logistics operators (see green marked cells), we still have 2 entries and 2 exits, where which are on differente rows.
In order to be able to overcome the problem, we'd need our query to return something like this:
Is there any possible way to create a query to return the results shown on the last above image? May anyone help?
P.S. If there's any other information need or if is there anything that's confusing, let me know