sql - How to take difference between current data and previous? -
i working on access 2010 table contains date, unit id, meter reading, correction. want take daily reading of meter taking current date , subtracting previous. here example of table have:
inputdate | unitid | meterreading | correction --------- | ------ | ------------ | ---------- 11/1/2016 | u1 | 41476 | 10000 11/1/2016 | u2 | 5991 | 10000 11/1/2016 | u3 | 164205 | 1000000 11/1/2016 | u4 | 11160 | 100000 11/2/2016 | u1 | 41490 | 10000 11/2/2016 | u2 | 5991 | 10000 11/2/2016 | u3 | 164205 | 1000000 11/2/2016 | u4 | 11169.3 | 100000 i've tried number of different ways. way got work make query split table each unit (e.g. u1, u2, etc.). query result , calculation:
( ([meterreading]+[correction]) - (dlookup("meterreading", "tbla", "[tbla].[inputdate] = #" & dateadd("d",-1,[tbla].[inputdate]) & "#")+[correction]) ) then query group data date.
my question if there more intuitive way this? i've looked @ ways try doing sql queries, nested queries, , grouping unit_id, calculation off. think dlookup not looking @ right unit when doing calculation since there multiple units on same day. also, "your query not include specified expression" when try group inputdate , unitid. i'm new sql language appreciated.
sample output be:
inputdate | unitid | meterruntime --------- | ------ | ------------ 11/2/2016 | u1 | 14 11/2/2016 | u2 | 0 11/2/2016 | u3 | 0 11/2/2016 | u4 | 9.3
if i'm understanding correctly following should give need.
select tbla.inputdate, tbla.unitid, ([tbla].[meterreading]+[tbla].[correction])- ([tbla_1].[meterreading]+[tbla_1].[correction]) meterruntime tbla inner join tbla tbla_1 on (tbla.inputdate-1 = tbla_1.inputdate) , (tbla.unitid = tbla_1.unitid);
Comments
Post a Comment