Row_number
を使用 あなたのテーブルを注文します。次に前の行と結合して比較します
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