Skip to content
Advertisement

Time difference between swipe-in and swipe-out for employees; allow for tailgating and exit on different day

This is continuation of/added complexity to the question: sql query – Get the difference time between swipe in – Swipe out for employee. We have similar swipe-in/out data, but additional constraints.

Sample source data:

The data comes in the above format but the desired output and input data increase the complexity.

Points to be considered are :

  1. There are two entry codes : EN1 , EN2 – representing two different offices , However both offices record exit with same code EX. Time spent at both offices account to the in time of the employee.

  2. There are cases where an employee can tailgate hence exit record corresponding to an entry might be missing (for example consider Row # 4 and 5 in above given sample data ), in those cases we have to highlight that row saying exit is missing/ invalid under VALIDITY column of output.

  3. An employee can enter the office premise on day 1 and exit it on day 2 (for example consider Row #8,9 in above given sample data) for such cases we should have 2 records/rows in the output: 1st row for day1 : entry time is swipe_time of event_type:’EN1 or EN2′ , exit time: 23:59 2nd row for day2 : entry time:00:00 , exit time is the swipe_time of event_type:’EX’

  4. Every record should have two fields DAY_HOURS CALENDER_HOURS. If an employee enters at 18:00 on day1 and exits at 3:00 on day2 , DAY_HOURS = 8 hours(complete hours of work) and CALENDER_HOURS = 6 hours (hours on that particular day)

Required Output: Table in below format:

Advertisement

Answer

With help of a friend , I solved the issue mentioned in Point # 2: by below steps ,As I wanted the solution only in sql script , with my little sql knowledge I designed the solution in 3 steps, I am sure it can be handled in a simpler way by experts :

Considering base table as EMP_DATA , I used one temp table : Temp_1 For base table I have added a “VALIDITY” field to flag the records at the final step.

EMP_DATA:

Step1:

below query will assign the rownumbers for rows in EMP_DATA and copy them to temp_1 table:

Step 1 Output:

Step2:

identify last event (previous event ) and next event for each event in the row of the temp_1 table

Step 2 Output:

Step 3:

Based on the LastEvent and NextEvent we can identify the tailgated entries and update the main table EMP_DATA by below query :

Final Output: *Step 3 Output:*

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement