If I have a table like follows:
Meter Serial Date | Reading
A 2017-01-01 10
B 2017-02-10 20
A 2017-03-05 20
B 2017-05-01 100
A 2017-06-01 300
Is it possible to get a query that displays like follows:
Meter | Date Start | Start Reading | Date End | End Reading
A 2017-01-01 10 2017-03-05 20
B 2017-02-10 20 2017-05-01 30
A 2017-03-05 20 2017-06-01 300
Note: The readings do not come daily. But they are unique for a given day (e.g. you cannot have two readings on the same day)
This is the current query I am working with:
with tbl as (select row_number() over(order by read_date) as rn, meter_serial, meter_channel, total_meter_read, read_date
from meter_reading_total)
select l.meter_serial, l.read_date, l.total_meter_read, r.read_date, r.total_meter_read
from tbl as l
left outer join
tbl as r
on r.rn = l.rn + 1
and r.meter_serial = l.meter_serial
and r.meter_channel = l.meter_channel