Find the time difference between two consecutive rows in the same table in sql

前端 未结 5 1424
广开言路
广开言路 2020-12-30 15:21

I\'m stuck. I\'ve looked for an answer, but can\'t seem to find subtracting time in the same table from two different rows of the same table that fits. I\'m having a difficu

5条回答
  •  长发绾君心
    2020-12-30 15:54

    WITH rows AS
    
    (
    
    SELECT  *, ROW_NUMBER() OVER (ORDER BY EmpID) AS rn        
    FROM    TimeSheet         
    )
    
    SELECT mc.EmpID, DATEDIFF(MINUTE, mc.TimeIn, mp.TimeOut) as TimeDiffInMinutes
    FROM    rows mc JOIN    rows mp ON   mc.rn = mp.rn-1
    

提交回复
热议问题