Utilisez Row_number
pour commander votre table. Rejoindre ensuite la ligne précédente pour comparer
with cte as (
select
*, row_number() over (partition by [Emp ID] order by time) rn
from
Timecal
)
select
a.[Emp ID], a.time, status = case when datediff(hh, b.time, a.time) < 5 then 0 else 1 end
from
cte a
left join cte b on a.[Emp ID] = b.[Emp ID] and a.rn - 1 = b.rn