Como se menciona en los comentarios, no puede restar marcas de tiempo con -
operador. Utilice DATEDIFF
en su lugar. Para obtener la diferencia de la fila actual y las marcas de tiempo de la fila siguiente, use OUTER APPLY
.
select t2._number,t2._timestamp,
datediff(microsecond,t2._timestamp,t1._timestamp) as diff
from dbo.tbl t2
outer apply (select t1._timestamp
from dbo.tcp t1
where t1._number = t2._number + 1) t1
Editar:Para update
una columna llamada diff según el comentario del OP,
with cte as (
select t2._number,t2._timestamp, t2.diff,
datediff(microsecond,t2._timestamp,t1._timestamp) as diff_col
from t t2
outer apply (select t1._timestamp
from t t1
where t1._number = t2._number + 1) t1
)
update cte set diff=diff_col;