Time series utilities overview
Functions for time bucketing, ordered selection, and time-based calculations
Time series utilities provide essential functions for working with time-series data, including bucketing data by time intervals, selecting values based on temporal ordering, and performing time-based calculations.
Samples
Section titled “Samples”Time bucketing
Section titled “Time bucketing”Bucket temperature readings into 5-minute intervals and calculate the average:
SELECT time_bucket('5 minutes', time) AS five_min, avg(temperature)FROM readingsGROUP BY five_minORDER BY five_min DESC;Ordered selection
Section titled “Ordered selection”Get the first and last temperature values for each device in 1-hour buckets:
SELECT device_id, time_bucket('1 hour', time) AS hour, first(temperature, time) AS first_temp, last(temperature, time) AS last_tempFROM readingsGROUP BY device_id, hourORDER BY hour DESC;Month normalization
Section titled “Month normalization”Normalize monthly sales metrics to account for varying month lengths:
SELECT time_bucket('1 month', sale_date) AS month, SUM(amount) AS total_sales, month_normalize(SUM(amount), time_bucket('1 month', sale_date)) AS normalized_salesFROM salesGROUP BY monthORDER BY month;Available functions
Section titled “Available functions”Time bucketing
Section titled “Time bucketing”time_bucket(): bucket rows by time interval to calculate aggregatestime_bucket_ng(): next generation time bucketing with additional features
Ordered selection
Section titled “Ordered selection”first(): get the first value in one column when rows are ordered by another columnlast(): get the last value in one column when rows are ordered by another column
Time utilities
Section titled “Time utilities”days_in_month(): calculate the number of days in a month given a timestampmonth_normalize(): normalize a monthly metric based on the number of days in the month