My table:Trnevents
emp_reader_id EVENTID DT
102 0 2018-01-04 15:57:04.000
102 0 2018-01-04 15:58:05.000
102 1 2018-01-04 16:46:19.000
102 0 2018-01-04 18:15:27.000
102 1 2018-01-04 18:20:47.000
102 0 2018-01-04 20:02:05.000
102 0 2018-01-04 21:47:29.000
102 1 2018-01-04 22:00:00.000
I used this query it works good but it get same out time
select
emp_Reader_id, cast(DT as date) [date]
, DT as check_in_1
, next_timestamp as check_out_1
from (
select
emp_Reader_id, DT, EVENTID, next_timestamp, next_EVENTID
, dense_rank() over(partition by emp_Reader_id, cast(DT as date) order by DT) in_rank
from trnevents t1
outer apply (
select top(1) t2.DT, t2.EVENTID
from trnevents t2
where t1.emp_Reader_id = t2.emp_Reader_id and t1.EVENTID <> t2.EVENTID
and cast(t1.DT as date) = cast(t2.DT as date)
and t1.DT < t2.DT
order by t2.DT
) oa (next_timestamp, next_EVENTID)
where EVENTID = '0'
) d
group by emp_Reader_id, cast(DT as date),DT,next_timestamp
order by emp_reader_id
Result:
emp_Reader_id date check_in_1 check_out_1
102 2018-01-04 2018-01-04 15:57:04.000 2018-01-04 16:46:19.000
102 2018-01-04 2018-01-04 15:58:05.000 2018-01-04 16:46:19.000
102 2018-01-04 2018-01-04 18:15:27.000 2018-01-04 18:20:47.000
102 2018-01-04 2018-01-04 20:02:05.000 2018-01-04 22:00:00.000
102 2018-01-04 2018-01-04 21:47:29.000 2018-01-04 22:00:00.000
Expected Output:
emp_Reader_id date check_in_1 check_out_1
102 2018-01-04 2018-01-04 15:57:04.000 ----
102 2018-01-04 2018-01-04 15:58:05.000 2018-01-04 16:46:19.000
102 2018-01-04 2018-01-04 18:15:27.000 2018-01-04 18:20:47.000
102 2018-01-04 2018-01-04 20:02:05.000 ----
102 2018-01-04 2018-01-04 21:47:29.000 2018-01-04 22:00:00.000
Is it possible to get above expected output.anyone could help. Thanks in advance