I have a table
Meter_Reading
MeterID | Reading | DateRead |
1 10 1-Jan-2012
1 20 2-Feb-2012
1 30 1-Mar-2012
1 60 2-Apr-2012
1 80 1-May-2012
The reading is a cumulative value where i would need to calculate the difference from the previous month and the current month.
Could you help me figure out how to generate a view where i can see the consumption (previous month reading – current month reading) for each month?
I had tried the between function:
select address, reading as Consumption, dateread
from ServiceAddress, reading, meter
where address like '53 Drip Drive%'
and dateread
between (to_date('01-JAN-2012','DD-MON-YYYY')) and (to_date('30-SEP-2012', 'DD-MON-YYYY'))
and serviceaddress.serviceaddid = meter.serviceaddid and meter.meterid = reading.meterid;
but all i got was the readings for each month not the difference.
How could I make it list the monthly consumption?
You can use the
LAGfunction to get the reading for the prior month. The query you posted references three tables–ServiceAddress,Reading, andMeternone of which are theMeter_Readingtable you posted the structure and data for. I’ll ignore the query you posted since I’m not sure what the data in those tables looks like and focus on theMeter_Readingtable that you posted data forI assume that if there is no prior reading that you want to assume that the prior reading was 0