Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Calcolo della differenza di orario in SQL Server

Usa Row_number per ordinare la tua tavola Quindi unisciti alla riga precedente per confrontare

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