These functions are useful for doing time series analysis by grouping the data into arbitrary intervals. See: https://blog.timescale.com/blog/simplified-time-series-analytics
-using-the-time_bucket-function/ for more examples.
There are two versions of the function:
- `time_bucket(<timestamp>, <interval>)`
Both functions accept a `BIGINT` timestamp and an interval in milliseconds as arguments. The `time_bucket_ns()` function accepts timestamps in nanoseconds and `time_bucket
()` accepts timestamps in milliseconds. Both return timestamps in the original format.
The query below calculates the average for the `cpu` metric for every five minute interval.
SELECT time_bucket(time_stamp, 30000) AS five_min, avg(cpu)
GROUP BY five_min
ORDER BY five_min DESC LIMIT 12;