← Graph

Window Functions

concept 4 connections

SQL feature using OVER (and optional PARTITION BY / ORDER BY) that applies a calculation per row while being aware of other rows. Often mistaken for data-science-only territory but 'freakishly powerful' for ordinary app code. Examples from the talk: average salary per department via `AVG() OVER (PARTITION BY department)`; cheapest product per category via `ROW_NUMBER() OVER (PARTITION BY category ORDER BY price, name)` (the extra sort key keeps ordering stable); cumulative sum via `SUM() OVER (ORDER BY … ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)`. All composable in Rails and much faster than equivalent Ruby post-processing; pair well with pattern matching when consumed in Ruby.

category
pattern
about
Window Functions concept
Dedicated section demonstrating window functions as a 'freakishly powerful' Postgres feature.
about
Window Functions concept
Explicitly about choosing window functions over Ruby-side aggregation.
recommends
Window Functions concept
Urges Rails devs to use window functions instead of two-query-plus-Ruby combining.
concept Window Functions
related_to
PostgreSQL tool
Demonstrated against Postgres.

Provenance

Read by
1 extraction