How to calculate hourly electricity consumption in QuestDB?

I have a table with electricity meter readings in QuestDB with the data as following:

SELECT timestamp, reading from energy_value


The timings of the data points are random. How can I calculate the hourly electricity consumption? I tried

select last(reading) - first(reading) as diff, timestamp
from energy_value
sample by 1h align to calendar

but it seems to be inaccurate, it misses the difference on the edge of the hour.

It can be done with first_value window function to get previous value

SELECT timestamp, value - prev_value as diff
  SELECT timestamp, value, 
     first_value(value)  OVER (ORDER BY timestamp ROWS 1 PRECEDING) as prev_value 
    select first(value) value, timestamp
    FROM device_frmpayload_data_energy_value
    SAMPLE by 1h align to calendar