Answer the question
In order to leave comments, you need to log in
How to get the average in different intervals in posgresql?
Good afternoon.
There is the following table
id | created | value
1 | 2021-01-01 10:00:00 | 1
2 | 2021-01-01 10:01:00 | 10
3 | 2021-01-01 10:02:00 | 33
4 | 2021-01-01 10:03:00 | 3
....
N | 2021-09-01 13:13:00 | 40
select to_created(CONCAT(
extract('year' from e."created"), '-',
extract('month' from e."created"), '-',
extract('day' from e."created"), '-', ' ',
extract('hour' from e."created"), ':',
FLOOR(extract('minute' from e."created") / 3) * 3, ':',
extract('second' from e."created")
), 'YYYY-MM-DD hh24:mi:ss') as tmp, AVG(e.value) avg_v from table e group by tmp order by tmp asc;
Answer the question
In order to leave comments, you need to log in
At intervals of 1 year/month/day/hour/minute:
select date_trunc('hour', e.created), avg(e.value) from table e group by 1 order by 1
[00:00-07:00)
[07:00-14:00)
[14:00-21:00)
[21:00-00:00) - следующий день
[00:00-07:00) - следующий день
[00:00-07:00)
[07:00-14:00)
[14:00-21:00)
[21:00-03:00) - следующий день
[03:00-10:00) - следующий день
...
select date_trunc('day', e.created) + (trunc(extract('hour' from e.created) / 7) * 7 || ' hour')::interval, avg(e.value) from table e group by 1 order by 1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question