SQL Server › Window Functions
How does the ROWS versus RANGE frame specification within an OVER clause affect the result of a window function like a running total?
AdvancedAnswer
The frame specification defines exactly which related rows are included in the window function's calculation relative to the current row, with ROWS defining the frame based on a physical number of rows and RANGE defining it based on a logical range of values in the ordering column, which can behave differently, particularly when there are duplicate values in the ordering column.
SELECT OrderId, OrderDate, Amount,
SUM(Amount) OVER (ORDER BY OrderDate ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningTotal
FROM Orders;Real-world example A financial report carefully specifies ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW for its running total calculation, ensuring the frame behaves predictably even when multiple orders happen to share the exact same order date.