Mid Window Functions SQL & Databases

How do you compute a running total in SQL Server?

Short answer: Use SUM(Amount) OVER (ORDER BY OrderDate ROWS UNBOUNDED PRECEDING). Prefer ROWS over RANGE when you want physical cumulative sums with ties on the order key.

Sample solution

T-SQL
SELECT OrderId, OrderDate, Amount,
       SUM(Amount) OVER (
           ORDER BY OrderDate, OrderId
           ROWS UNBOUNDED PRECEDING
       ) AS RunningTotal
FROM Orders;
Mention PARTITION BY CustomerId for per-customer running totals.
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details