我需要帮助来根据每月获取的当前值计算电表的消耗
我有以下表格:
地址(PostCode int, Street varchar)
Meter(MeterID int, InstallationDate date, PostCode int)
Reading(ReadNo int, ReadDate date, ReadValue int, MeterID int, ReaderID int)
我尝试了以下方法:
Select ReadDate,
ReadValue AS CurrentValue,
lag(readvalue, 1, 0) OVER (ORDER BY READVALUE) AS PreviousValue,
readvalue - lag(readvalue, 1, 0) OVER (ORDER BY READVALUE) AS Consumption
From Reading R,
Meter M,
Address A
Where R.MeterID = M.MeterID
AND A.PostCode = M.PostCode AND A.PostCode = '1005'
Order By ReadDate;
当地址的电表被新电表替换时会出现问题,有人可以帮我解决这个问题吗?