liveBook · Manning

Window frame

As explained in DuckDB in Action

A window frame is the set of rows positioned relative to the current row over which a window function is evaluated.

A window frame selects the rows positioned relative to the current row for evaluating a window function. The frame moves through an ordered partition, using preceding or following rows defined by a row count or range expression. section 4.5.2

A frame supports practical analysis patterns:

  • Specify a centered seven-day frame with three days preceding, the current day, and three days following. section 4.5.2
  • Combine PARTITION BY system_id with ORDER BY day to calculate a moving average independently for each system. section 4.5.2
  • Choose row-based distances or range-based intervals from the ordering expression. section 4.5.2
Listing 4.17 Using a range partition for applying a window function
SELECT system_id,
       day,
       kWh,
       avg(kWh) OVER (
            PARTITION BY system_id  #1
            ORDER BY day ASC #2
            RANGE BETWEEN INTERVAL 3 Days PRECEDING  #3
                      AND INTERVAL 3 Days FOLLOWING
       ) AS "kWh 7-day moving average"
FROM v_power_per_day
ORDER BY system_id, day;

Inspect a complete SQL window definition for a seven-day, range-based moving average over daily energy readings. section 4.5.2

How DuckDB in Action covers it

How deep it goes
Defines row and range boundaries and demonstrates a centered seven-day frame, stopping short of broader frame variations.
Examples
Demonstrates daily energy readings, system partitions, and a seven-day moving average using a range frame.
What it assumes
Assumes familiarity with window functions, partitions, ordering, aggregation, and daily energy data.
What you can do after
Apply it via SQL to calculate moving averages over row-based or range-based windows.
How it connects
Connects window frames with window functions, partitions, ordering, aggregate functions, and moving averages.

Also explained in

Compare how 2 books explain window frame →

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.