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.
- 类别
- 窗口
- Returns
frame boundary- In this category
- 54 of 79
语法
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW | RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
引擎支持
| 引擎 | Support | 备注 |
|---|---|---|
| SQL Server | 是 | Supported under this name. |
| PostgreSQL | 是 | Supported under this name. |
| MySQL | 是 | MySQL 8.0+ |
| SQLite | 是 | SQLite 3.25+ |
| Oracle | 是 | Supported under this name. |
示例
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;
值得了解
- 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.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
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.