T-SQL reference › Running total

Running totals in SQL Server with SUM() OVER

SUM() OVER adds as it goes. You keep the detail rows. No cursor. No triangular join.

Daily take, running total

SELECT CAST(order_date AS date) AS day,
       SUM(total_amount) AS revenue,
       SUM(SUM(total_amount)) OVER (ORDER BY CAST(order_date AS date)) AS running
FROM Orders
WHERE status = 'Shipped'
GROUP BY CAST(order_date AS date)
ORDER BY day;

The inner SUM is per day. The window SUM walks those daily totals in date order. Leave ROWS UNBOUNDED PRECEDING implicit unless you need to be exact about ties.

Try it on Harbour & Co

Amy Nair (Finance Lead) needs this now: “Daily revenue for the last 14 days, not today, with a running total.” Write the query, run it, send the results. On the paid plan.

Take the ticket →

Learn it on a real ticket

Monday is free on WorkSQL. A colleague’s ticket lands, you write the T-SQL, we mark it. No videos. Five minutes.

Clock in free See Monday’s tickets