I have a table like below which contains temperature data for the last 6 months. It takes about 30-40 readings of temperature in an hour.
‘time’ is in epoch milliseconds.
time | temperature |
---|---|
1678270897857 | 25 |
1678270777712 | 30 |
1678270657548 | 38 |
1678270537402 | 27 |
1678270417258 | 38 |
… | … |
I need a query in PostgreSQL to get the ‘temperature’ averaged per hour per day for the last 30 days (2023-01-15 till 2023-02-15).
So the expected result should look something like:
date | average_temp |
---|---|
2023-01-15 01:00:00 | 28.5 |
2023-01-15 02:00:00 | 32.6 |
2023-01-15 03:00:00 | 21.9 |
… | … |
2023-01-15 23:00:00 | 33.8 |
2023-01-16 00:00:00 | 26.1 |
2023-01-16 01:00:00 | 29.4 |
… | … |
2023-02-15 22:00:00 | 31.3 |
2023-02-15 23:00:00 | 22.7 |
2
Answers
You could try to use below query.
start date
toend date
by using generate_series function, timeframe per each record is 1 hour.See demo here
You could convert the times to timestamps using to_timestamp then use date_trunc to ’round’ to hour.