// window reference

SQL CURRENT ROW frame boundary

As a frame boundary, CURRENT ROW is positional under ROWS and peer-inclusive under RANGE and GROUPS, which is where identical queries diverge.

Catégorie
Fenêtre
Returns
frame boundary
In this category
54 of 79

Syntaxe

ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW | RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW

Prise en charge des moteurs

MoteurSupportNotes
SQL Server Oui Supported under this name.
PostgreSQL Oui Supported under this name.
MySQL Oui MySQL 8.0+
SQLite Oui SQLite 3.25+
Oracle Oui Supported under this name.

Exemples

Any engine

-- tied days: RANGE version includes both rows' amounts at once
SELECT day, amount,
       SUM(amount) OVER (ORDER BY day ROWS UNBOUNDED PRECEDING) AS by_row,
       SUM(amount) OVER (ORDER BY day RANGE UNBOUNDED PRECEDING) AS by_peer
FROM t;

Bon à savoir

  • With duplicate ORDER BY values, RANGE ... CURRENT ROW includes every peer, so running sums jump by the whole tie group; ROWS ... CURRENT ROW walks row by row.
  • This single word difference is the whole ROWS-vs-RANGE distinction; the default (implicit) frame uses RANGE semantics, which is where most surprises originate.
  • CURRENT ROW works as both the start and the end boundary; as a start it means 'this row onward' (plus peers under RANGE).

Questions about CURRENT ROW Boundary

Why does my running total skip ties one moment and include them the next?

Someone changed ROWS to RANGE (or removed it, falling back to the default RANGE). Under RANGE, CURRENT ROW carries the current row's peers with it.

Which CURRENT ROW should I use for dedupes and top-N?

Neither directly: ranking uses partitions, not frames. For running aggregates over unique-ordered rows, prefer ROWS plus a tiebreaker.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.