DATE_BUCKET
Date bucketing is accomplished through the use of theDATE_BUCKET
function, which calculates the date range in which a given timestamp falls,
based on a set of fixed-width “buckets”, start-aligned to a date/time, and
offset from that start date/time.
The basic form of the DATE_BUCKET function is:
width
width
The number of days each bucket should span. An
INTERVAL can also be used to specify
the width.timestamp
timestamp
A date/time column or expression, the date portion of which will be used in placing the
corresponding record within the correct date bucket.
offset
offset
The number of days after (for a positive
offset) or number of days before
(for a negative offset) the base date/time to which the buckets should be aligned. An
INTERVAL can also be used to specify the offset. The default is no offset.base
base
The starting date/time to which buckets will be aligned. The default is
2000-01-03.Example
Typically,DATE_BUCKET is used in the following type of query:
DATE_BUCKET Example
- Dates in the
dscolumn of theexample.host_metrics_summarytable will be grouped into buckets - Each bucket will span a range of
7days - The baseline bucket will start at
2023-02-18(2023-02-21offset by-3days) and continue through2023-02-24(7days, including2023-02-18) - Buckets will extend before & after the baseline bucket in contiguous, non-overlapping fashion
- Each result record will show the date in the middle of the bucket’s date range (
+ INTERVAL 3 DAYSfrom the start of each7day span) and the average CPU usage across the records contained within that date range - Gaps in the data will not be filled in with empty buckets—only buckets containing the dates found in
the
dscolumn ofexample.host_metrics_summarywill be returned in the result set