Windows II — frames & running
Concept  ·  SQL & Querying

EXCLUDE CURRENT ROW / TIES refine which frame rows contribute (H-tier garnish).

Self-inclusive baselines flatter — a reading compared to an average containing itself is pulled toward normal, most strongly in small windows where anomalies matter most. EXCLUDE CURRENT ROW states the intent in the frame; EXCLUDE TIES/GROUP extend it to peer rows sharing the sort value. Support is sparse (DuckDB yes); the expansion is arithmetic: (SUM(x) OVER w - x) / NULLIF(COUNT(*) OVER w - 1, 0).

See it break
v
dx
110
220
330
The trap
SELECT
  d,
  AVG(x) OVER (
    ORDER BY
      d ROWS BETWEEN 1 PRECEDING
      AND 1 FOLLOWING ) AS peer_avg
FROM
  v
ORDER BY
  d;
The fix
SELECT
  d,
  AVG(x) OVER (
    ORDER BY
      d ROWS BETWEEN 1 PRECEDING
      AND 1 FOLLOWING EXCLUDE CURRENT ROW
  ) AS peer_avg
FROM
  v
ORDER BY
  d;
INPUTYour queryThe fix
11520
22020
32520

Each row's peer average included itself, pulling it toward normal — EXCLUDE CURRENT ROW trims the row out of its own frame, so the middle row's peers average a true 20, not a self-inflected 20.

The rule

EXCLUDE where supported; the subtract-self expansion elsewhere.

EXCLUDE support is sparse (DuckDB yes, many engines no) — the subtract-self expansion is the portable form.

Shows up elsewhere
Default frame includes peer tiesCOALESCE / NULLIF mechanics
Try one →Prove it in practice →