It is apparent from your sample data , you have 2 rows of data per user.
row_number
will work for you. Subtracting rn2-rn1 will return required output
with cte as
(select [ClockNo] ,
[FirstName] ,
[LastName] ,
[Department] ,
[ClockPoint] ,
[Date] ,
time,
ROW_NUMBER() over partition by clockno,firstname order by date,time) rn
from mytable
)
select c.[ClockNo] ,
c.[FirstName] ,
c.[LastName] ,
c.[Department] ,
c.[ClockPoint] ,
c.[Date] ,
c1.time -c.time
from cte c inner join (select * from cte where rn= 2) c1
on c.rn = c1.rn-1 and c.firstname = c.firstname and c.clockno = c1.cloclno
where c.rn = 1
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…