Subtracting one row of data from another in SQL

前端 未结 7 756
抹茶落季
抹茶落季 2020-12-09 19:06

I\'ve been stumped with some SQL where I\'ve got several rows of data, and I want to subtract a row from the previous row and have it repeat all the way down.

So her

7条回答
  •  悲&欢浪女
    2020-12-09 19:30

    What about something like this:

    SELECT T2.ID, T2.[Length], T2.[Length]-T1.[Length] AS 'Difference'
    FROM Foo AS T1 RIGHT OUTER JOIN Foo AS T2 ON ( T1.ID = (T2.ID-1) )
    ORDER BY T1.ID
    

提交回复
热议问题