time_bucket function is similar to the standard date_bin
function. Unlike date_bin, it allows for arbitrary time intervals of months or
longer. The return value is the bucket’s start time.
Buckets are aligned to start at midnight in UTC+0. The time bucket size (bucket_width) can be set as INTERVAL or
INTEGER. For INTERVAL-type bucket_width, you can change the time zone with the optional timezone parameter. In this
case, the buckets are realigned to start at midnight in the time zone you specify.
Note that during shifts to and from daylight savings, the amount of data
aggregated into the corresponding buckets can be irregular. For example, if the
bucket_width is 2 hours, the number of bucketed hours is either three hours or one hour.
Samples
Simple five-minute averaging:2017-12-31, a Sunday
within the period being analyzed. However, the origin provided to the function
can be before, during, or after the data being analyzed. All buckets are
calculated relative to this origin. So, in this example, any Sunday could have
been used. Note that because time < TIMESTAMPTZ '2018-01-03' is used in this
example, the last bucket would have only 4 days of data. This cast to TIMESTAMP
converts the time to local time according to the server’s time zone setting.
Arguments
The syntax is:
For integer time inputs:
If you use months as an interval for
bucket_width, you cannot combine it with
a non-month component. For example, 1 month and 3 months are both valid
bucket widths, but 1 month 1 day and 3 months 2 weeks are not.