liveBook · Manning

Bucket

As explained in DuckDB in Action

A time bucket is a fixed-width temporal interval to which timestamped observations are assigned for grouped analysis.

A time bucket is a fixed-width temporal interval that groups timestamped observations for analysis. DuckDB’s time_bucket() truncates timestamps to an interval and can align them with an optional offset, making inconsistent sensor intervals comparable. section 4.1

Aggregate sensor readings during ingestion

This query assigns readings to 15-minute buckets, replaces negative or missing power values with zero, averages each group, and inserts the result into readings. section 4.1

Listing 4.1 Cleaning and transforming data during ingestion
INSERT INTO readings(system_id, read_on, power)
SELECT any_value(SiteId), #1
       time_bucket(
           INTERVAL '15 Minutes',
           CAST("Date-Time" AS timestamp)
       ) AS read_on, #2
       avg(
           CASE
               WHEN ac_power < 0 OR ac_power IS NULL THEN 0
               ELSE ac_power END)  #3
FROM
    read_csv_auto(
        'https://developer.nrel.gov/api/pvdaq/v3/' ||
        'data_file?api_key=DEMO_KEY&system_id=10&year=2019'
    )
GROUP BY read_on
ORDER BY read_on;

Inspect the complete DuckDB SQL pattern for reading CSV data, casting timestamps, bucketing observations, cleaning values, grouping, and ordering results. section 4.1

Use buckets to:

  • Normalize data arriving at different intervals, such as one-minute and five-minute measurements. section 4.1
  • Remove already-ingested readings outside the intended quarter-hour boundaries with date_part and DELETE. section 3.4.3

How DuckDB in Action covers it

How deep it goes
Explains timestamp truncation, optional alignment, grouping, cleaning, and post-ingestion cleanup, but defers broader date-time functions.
Examples
Demonstrates one-minute and five-minute source files, 15-minute power averages, and quarter-hour DELETE cleanup.
What it assumes
Assumes basic SQL and ingested sensor tables while demonstrating timestamp casting, CASE expressions, grouping, and averaging.
What you can do after
Apply it via DuckDB SQL to group timestamped readings into fixed intervals during ingestion.
How it connects
Connects time buckets with timestamps, GROUP BY, aggregate functions, data cleaning, and ingestion.

Also explained in

Compare how 9 books explain bucket →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of DuckDB in Action. The section links point to the parts of the book it draws on.