Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i get the records of my night and day shift with multiple records in attendance.

Verify_State identify when 0 = Time in and 1 = Time out


What i have tried Queries are here in this. Fiddle[^]

Where the problem is the user_id of 14 is a day shift and the user_id of 15 is night shift that i can't get the attendance correctly.

Exptected Output :
user_id  verify_date      verify_state  prev_state  next_state    next_date
14    2019-11-05 07:23:00       0           1           1   2019-11-05 20:00:00
14    2019-11-06 07:23:00       0           1           0   2019-11-07 20:09:00
14    2019-11-08 07:18:00       0           1           1   2019-11-08 17:38:00
14    2019-11-11 07:20:00       0           1           1   2019-11-11 20:05:00
15    2019-11-12 19:57:00       0           1           0   2019-11-13 07:20:00


What I have tried:

This is the Query i have tried :
with 
cte1 as ( select *, 
                 coalesce(lag(verify_state) over (partition by user_id order by verify_date asc),1) prev_state,
                 coalesce(lead(verify_state) over (partition by user_id order by verify_date asc),0) next_state
          from tbl_excel_attendance
),
cte2 as ( select *, 
                 lead(verify_date) over (partition by user_id order by verify_date asc) next_date
          from cte1 
          where (prev_state,verify_state,next_state) in ( (1,0,0),(1,0,1), (0,1,0) )
)
select * 
from cte2 
where verify_state = 0
  and next_date is not null
order by 2,3


but if the records was so many it will not get the right record should it be. like the expected output.
Posted
Updated 15-Nov-19 0:14am
v2

1 solution

The shift is not what is stopping you getting the query correct, it's the underlying data.
For example look at employee 14's first 6 records in chronological order:
user_id	verify_date	verify_state
14	2019-11-05 07:23:00.000	0
14	2019-11-05 20:00:00.000	1
14	2019-11-06 07:23:00.000	0
14	2019-11-06 19:32:00.000	0
14	2019-11-07 07:25:00.000	0
14	2019-11-07 07:25:00.000	0
Employee 14 did not clock out between 2019-11-06 07:23:00.000 and 2019-11-06 19:32:00.000. I.e the pattern is IN,OUT,IN,IN,IN,IN.

You are going to have to make some decisions about what to do when the clocking out data is not available.

E.g. assume a maximum shift length and derive a sequence of IN,OUT,IN,OUT inserting missing OUT records with assumed values.

To use that method create a sequence (use a CTE or temporary table) of expected shift start and end dates and times covering the entire range of dates and times you are interested in. Manas gives some examples in this Tip - Generating a Sequence in SQL[^]
Then use that sequence with a left outer join to the data you do have or insert the missing data into your base table
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900