Just try:
SELECT time_bucket('5 minutes', time) AS five_min, array_agg(cpu) ...
Remember that it’s all about learning how group by works. check a small generated series:
tsdb=> select a from generate_series(now(), now()+interval '1 min', interval '20 s') a;
a
-------------------------------
2024-04-25 13:27:01.436482+00
2024-04-25 13:27:21.436482+00
2024-04-25 13:27:41.436482+00
2024-04-25 13:28:01.436482+00
(4 rows)
Now let’s build a bucket of 30 seconds:
tsdb=> select time_bucket('30 seconds',a) from generate_series(now(), now()+interval '1 min', interval '20 s') a;
time_bucket
------------------------
2024-04-25 13:27:00+00
2024-04-25 13:27:30+00
2024-04-25 13:28:00+00
2024-04-25 13:28:00+00
(4 rows)
now let’s use array_agg to explore:
tsdb=> select time_bucket('30 seconds',a), array_agg(1) from generate_series(now(), now()+interval '1 min', interval '20 s') a;
ERROR: column "a.a" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: select time_bucket('30 seconds',a), array_agg(1) from genera...
^
Note that aggregation functions combined with other tuples forces you to group by:
tsdb=> select time_bucket('30 seconds',a), array_agg(1) from generate_series(now(), now()+interval '1 min', interval '20 s') a group by 1;
time_bucket | array_agg
------------------------+-----------
2024-04-25 13:27:30+00 | {1}
2024-04-25 13:28:30+00 | {1}
2024-04-25 13:28:00+00 | {1,1}
(3 rows)
Note that you can also use elements in the array_agg:
tsdb=> select time_bucket('30 seconds',a), array_agg(a) from generate_series(now(), now()+interval '1 min', interval '20 s') a group by 1;
time_bucket | array_agg
------------------------+-------------------------------------------------------------------
2024-04-25 13:27:30+00 | {"2024-04-25 13:27:53.916687+00"}
2024-04-25 13:28:30+00 | {"2024-04-25 13:28:33.916687+00","2024-04-25 13:28:53.916687+00"}
2024-04-25 13:28:00+00 | {"2024-04-25 13:28:13.916687+00"}