Given a table of cars and their odometer reading at various dates (first of each month), how can I write TSQL (ideally, for use as a SQL Server view) to return the "incremental" values?
In other words, I want the reverse operation from Calculate a Running Total in SQL Server.
Example:
On this table:
CarId | Date | Mileage
---------------------------
1 1/1/2000 10000
1 2/1/2000 11000
1 3/1/2000 12000
2 1/1/2000 10000
2 2/1/2000 11001
2 3/1/2000 12001
3 1/1/2000 10000
(missing datapoint for (3, 2/1/2000))
3 3/1/2000 12000
We'd return something like (the details/edge cases are flexible):
CarId | Date | Delta
---------------------------
1 1/1/2000 10000
1 2/1/2000 1000
1 3/1/2000 1000
2 1/1/2000 10000
2 2/1/2000 1001
2 3/1/2000 1000
3 1/1/2000 10000
3 3/1/2000 2000