I am having difficulties subtracting data from the same column. My data structure looks like this:
TimeStamp ID state Gallons
1/01/2012 1 NY 100
1/05/2012 1 NY 90
1/10/2012 1 NY 70
1/15/2012 1 NY 40
So I would like to get the result of (100-40) = 60 gallons used for that time period. I tried using the following query:
SELECT T1.Gallons, T1.Gallons -T1.Gallons AS 'Gallons used'
FROM Table 1 AS T1
where Date = '2012-07-01'
ORDER BY Gallons
It seems like a simple way of doing this. However, I can not find an efficient way of including the timestamp as a way of retrieving the right values (ex. gallon value on 1/1/2012 – gallon value on 1/30/2012).
ps. The value of “Gallons” will always decrease
1 Answer