sql >> Base de Datos >  >> RDS >> Sqlserver

Cálculo de diferencia horaria en SQL Server

Usar Row_number para pedir tu mesa. Luego únase a la fila anterior para comparar

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