liveBook · Manning

Window frame

As explained in Just Use Postgres!

A window frame is the ordered subset of rows within a window partition that contributes to a window-function calculation for a row.

A window frame is the ordered subset of rows that contributes to a window-function calculation for the current row. PARTITION BY groups rows into windows; ORDER BY divides each window into progressively larger frames for running totals. section 3.5

Running play-duration total

For song_id = 2, ordering by user_id makes each frame include the current user's rows and preceding users' rows. The resulting totals are 144, 350, 654, and 654 seconds. Duplicate rows for user_id = 3 share the same frame total. section 3.5

Listing 3.13 Calculating running totals and total duration
SELECT song_id, user_id, play_duration, SUM(play_duration) 
OVER (PARTITION BY song_id ORDER BY user_id) AS total_play_duration
FROM streaming.plays
WHERE song_id = 2;

Run the query to calculate a running total within a song partition, ordered by user_id. section 3.5

How Just Use Postgres! covers it

How deep it goes
Explains partitions, ordering, frame contents, and resulting totals, but does not cover explicit frame-bound syntax.
Examples
Uses streaming-play records for song 2, ordered users, and cumulative totals of 144, 350, and 654 seconds.
What it assumes
Assumes familiarity with SQL aggregation and introduces frames as a further division of PARTITION BY windows.
What you can do after
Apply window-frame syntax to calculate ordered cumulative totals in SQL.
How it connects
Connects window frames with window functions, PARTITION BY, ORDER BY, and running totals.

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 Just Use Postgres!. The section links point to the parts of the book it draws on.