Explore Library
Code Quiz

Running Total With Duplicate Dates

A running-total window query silently misbehaves when the ORDER BY column has duplicate values.

Codesql
-- Goal: per-row running total of daily order amounts
SELECT
  order_id,
  order_date,
  amount,
  SUM(amount) OVER (ORDER BY order_date) AS running_total
FROM orders
ORDER BY order_date, order_id;

-- Sample rows share the same order_date, yet rows
-- with the same date all report an identical running_total.

Why do rows sharing an order_date get the same running_total instead of a true row-by-row cumulative sum, and how do you fix it?

Watch the deep dive

Watch on YouTube →