I have a table like this:
10/10/2018 00:00 | 5
10/10/2018 00:10 | 7
10/10/2018 00:20 | 9
...
10/10/2018 23:40 | 5
10/10/2018 23:50 | 6
I need to get average number in hour, and return all hour averages (from 00:00 to 23:00)
I can get two hour averages like this:
(select avg(value)
from public.table
where time_stamp between TO_TIMESTAMP('2018-11-05 20:00:00', 'YYYY-MM-DD HH24:MI:SS')
and
TO_TIMESTAMP('2018-11-05 20:00:00', 'YYYY-MM-DD HH24:MI:SS') + interval '1 hour')
UNION ALL
(select avg(value)
from public.table
where time_stamp between TO_TIMESTAMP('2018-11-05 21:00:00', 'YYYY-MM-DD HH24:MI:SS')
and
TO_TIMESTAMP('2018-11-05 21:00:00', 'YYYY-MM-DD HH24:MI:SS') + interval '1 hour')
But how can I got all 24 average values in some loop? I dont want to copy this SQL 24 times.
select date_trunc('hour', time_stamp), avg(value) from public.table where date_trunc('day', time_stamp) = date_trunc('day', TO_TIMESTAMP('2018-11-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS')) group by date_trunc('hour', time_stamp);