Calculating difference in column value from one row to the next

梦想与她 提交于 2020-01-30 13:14:27

问题


I am using an Access DB to keep track of Utility Usage on hundreds of accounts. The meters in these accounts have consumption values only per month. I need to take the consumption value of this month and subtract it from the value of the previous month. to get the consumption for this month. I know that in SQL Server, there is a lead/lag function that can calculate those differences. Is there a similar function in access? or is there a simple way to subtract the value in one row from the one above it?

Ex.

The first Line is Billed Date
The Second Line is the Meter Reading
The Third Line is Consumption

1/26/2014
2/25/2014
3/27/2014
4/28/2014
5/26/2014
7/29/2014

0
3163
4567
5672
7065
8468

1538
1625
1404
1105
1393
1403

回答1:


I do not quite get some of your results, but I think you want something like:

SELECT Meters.MeterDate, 
     Meters.MeterReading, 
    (SELECT TOP 1 MeterReading 
     FROM Meters m WHERE m.MeterDate <Meters.MeterDate 
     ORDER BY MeterDate DESC) AS LastReading, 
     [MeterReading]-Nz([LastReading],0) AS MonthResult
FROM Meters
ORDER BY Meters.MeterReading;



来源:https://stackoverflow.com/questions/27383816/calculating-difference-in-column-value-from-one-row-to-the-next

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!