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

前端 未结 5 1428
广开言路
广开言路 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:53

    Since you have mentioned PARTITION clause, given below is a version using that clause (haven't tested for syntax, but it should give you the idea)

    ;WITH EmpData AS
    (
        SELECT  EmpID, 
                    TimeIn, 
                    TimeOut,
                    ROW_NUMBER() OVER(PARTITION BY EmpId ORDER BY TimeIn) Position
           FROM EmployeeTime 
    )
    SELECT a.*
             a.TimeOut-b.TimeIn OutTIme 
       FROM EmpData a  LEFT JOIN EmpData b
             ON a.EmpId = b.EmpId
          AND a.Position-1  = b.Position  
    

提交回复
热议问题