I have an employee_attendance table (SQL Server 2012
) with the following columns:
Here is the SQL Fiddle with some sample data: http://sqlfiddle.com/#!3/ba8a1/1
I know that these employees can work on any day on any of the 3 shifts we have:
So what I need is to know for each employee in a selected day (the sample data has just one day and has been filtered to show only IN records, so only IN access to the company) is the first
Timestamp
that is near (below or above) the Shift entrance.
Here is an image of what the SQL Fiddle data should look like after applying the right script:
Since you have SQL Server 2012, you can use TimeFromParts and "mod 8" everything to minimize the work...
select employeeId,
accessCode,
minDiff = MIN(ABS(DATEDIFF(TIMEFROMPARTS(DATEPART(HH,t.timestamp) % 8,
(DATEPART(MI,t.timestamp),
(DATEPART(S,t.timestamp),0,0),
TIMEFROMPARTS(7,
(DATEPART(MI,t.timestamp),
(DATEPART(S,t.timestamp))
from table t
were t.timestamp is in a given daily range
So, since your shift changes (7,15,23) are all "mod 8 = 7" I just compare the hour portion of the timestamp "mod 8" to "7" -- that way I don't need to run the query three times.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With