Running Total Jumps When Order Keys Tie

A window ORDER BY with tied keys uses the default RANGE frame, so peer rows enter the frame together and the running total jumps instead of ticking.

SqlData Integrity

Code

snippet
-- 窗口 ORDER BY 出现并列键时,默认帧是 RANGE:
-- CURRENT ROW 在 RANGE 语义下"含全部 peer 行",并列的一起进帧
-- 结果:运行总额在这些行上一次性跳过所有并列值,而不是逐行累加
WITH ledger (id, posted_at, cents) AS (
    VALUES (1, TIMESTAMP '2026-11-01 09:00', 100),
           (2, TIMESTAMP '2026-11-01 09:05', 200),
           (3, TIMESTAMP '2026-11-01 09:05', 300),  -- 与 id=2 同一时刻
           (4, TIMESTAMP '2026-11-01 09:10', 400)
)
SELECT
    id,
    posted_at,
    cents,
    SUM(cents) OVER (ORDER BY posted_at) AS jumps_at_ties,
    SUM(cents) OVER (ORDER BY posted_at
        ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS row_by_row
FROM ledger
ORDER BY id;

-- id=2 与 id=3:jumps_at_ties 都是 600(200 与 300 同时计入)
-- row_by_row 则分别为 300 与 600:逐行累加需要 ROWS 帧或唯一 tiebreak

The default frame at ties

With ORDER BY posted_at and ties at 09:05 (ids 2 and 3), the default frame is RANGE, where CURRENT ROW includes all peer rows. Both tied rows therefore report the same running total 600 — the sum jumps from 100 to 600 in one step.

The ROWS alternative

The second window pins ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, so the tied rows tick individually: 300 then 600. Same data, different frame, different answer.

The design choice

To make the running total tick without ROWS, the ORDER BY needs a unique tiebreaker (id after posted_at); otherwise peers aggregate together by spec — not a bug, the documented frame semantics.

Interactive Tools for This Topic

FAQ

Why does the running total jump at tied timestamps?
The default window frame is RANGE, so CURRENT ROW includes every peer row; all tied amounts enter the frame together and the sum jumps once instead of ticking.
How do you force row-by-row accumulation?
Either pin the frame to ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, or add a unique tiebreaker to the ORDER BY so no peers remain.