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

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

    I have solved this for similar problems and it need not be that the rows even be sorted:

    select t1.EmpID, t1.TimeIn, t1.TimeOut, 
           datediff(minute, max(t2.TimeOut), t1.TimeIn) as minutes
    from timesheet t1 left join timesheet t2 on t1.EmpID = t2.EmpID 
           and t2.TimeOut < t1.TimeIn
    group by t1.EmpID, t1.TimeIn, t1.TimeOut
    

    Let me know if this works.

    Here is a sql fiddle: http://sqlfiddle.com/#!3/89a43/1

提交回复
热议问题