Explore Library
Databases & SQLWindow Functions

32 items

Code Quiz

Running Total With Tied Dates

A running-sum feature silently breaks when multiple rows share the same ORDER BY value due to the default RANGE frame.

Codesql
-- Feature: running total of spend per customer
SELECT
  customer_id,
  order_date,
  amount,
  SUM(amount) OVER (
    PARTITION BY customer_id
    ORDER BY order_date
  ) AS running_total
FROM orders
ORDER BY customer_id, order_date;

When several orders for a customer share the same order_date, running_total repeats the same value for all of them instead of accumulating row by row. What is the bug?