SQL Server Query to find running total, preceding rows, previous order, next order

SQL Server ROWS, RANGE, PRECEDING, and FOLLOWING

SQL query that covers the examples together: running total, ROWS, RANGE, preceding rows, previous order, next order, and a moving total.

USE AdventureWorks2022;
GO
SELECT
CustomerID,
OrderDate,
SalesOrderID,
TotalDue,
-- 1. Default running total
-- Because ORDER BY is present and frame is omitted,
-- SQL Server uses RANGE ... CURRENT ROW behavior.
SUM(TotalDue) OVER
(
PARTITION BY CustomerID
ORDER BY OrderDate
) AS RunningTotal_Default,
-- 2. RANGE running total
-- Same OrderDate values are treated as peers.
SUM(TotalDue) OVER
(
PARTITION BY CustomerID
ORDER BY OrderDate
RANGE BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW
) AS RunningTotal_RANGE,
-- 3. ROWS running total
-- Calculates row by row.
SUM(TotalDue) OVER
(
PARTITION BY CustomerID
ORDER BY OrderDate, SalesOrderID
ROWS BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW
) AS RunningTotal_ROWS,
-- 4. Previous 3 orders + Current order
-- Maximum 4 rows
SUM(TotalDue) OVER
(
PARTITION BY CustomerID
ORDER BY OrderDate, SalesOrderID
ROWS BETWEEN 3 PRECEDING
AND CURRENT ROW
) AS Previous3AndCurrent,
-- 5. Previous 3 orders only
-- Current order is NOT included
SUM(TotalDue) OVER
(
PARTITION BY CustomerID
ORDER BY OrderDate, SalesOrderID
ROWS BETWEEN 3 PRECEDING
AND 1 PRECEDING
) AS Previous3Orders,
-- 6. Previous order only
SUM(TotalDue) OVER
(
PARTITION BY CustomerID
ORDER BY OrderDate, SalesOrderID
ROWS BETWEEN 1 PRECEDING
AND 1 PRECEDING
) AS PreviousOrder,
-- 7. Next order only
SUM(TotalDue) OVER
(
PARTITION BY CustomerID
ORDER BY OrderDate, SalesOrderID
ROWS BETWEEN 1 FOLLOWING
AND 1 FOLLOWING
) AS NextOrder,
-- 8. Previous 2 orders + Current order
-- 3-order moving total
SUM(TotalDue) OVER
(
PARTITION BY CustomerID
ORDER BY OrderDate, SalesOrderID
ROWS BETWEEN 2 PRECEDING
AND CURRENT ROW
) AS Last3OrdersTotal,
-- 9. Previous 2 orders + Current order
-- 3-order moving average
AVG(TotalDue) OVER
(
PARTITION BY CustomerID
ORDER BY OrderDate, SalesOrderID
ROWS BETWEEN 2 PRECEDING
AND CURRENT ROW
) AS Last3OrdersAverage
FROM Sales.SalesOrderHeader
WHERE CustomerID = 11000
ORDER BY
OrderDate,
SalesOrderID;

Understand the keyword used with Partition by clause in SQL Server.

PRECEDING = rows before current row

CURRENT ROW = current row

FOLLOWING = rows after current row

UNBOUNDED PRECEDING = start of partition

UNBOUNDED FOLLOWING = end of partition

Easy to understand:

PRECEDING = go backward
FOLLOWING = go forward
CURRENT ROW = this row
UNBOUNDED PRECEDING = go back to the beginning
ROWS = count row positions
RANGE = consider equal ORDER BY values as peers at the frame boundary.

Running Data

You see output of sum of total due in adding one by one as running data in RunningTotal_default

SELECT
CustomerID,
OrderDate,
SalesOrderID,
TotalDue,
SUM(TotalDue) OVER ( PARTITION BY CustomerID ORDER BY OrderDate) AS RunningTotal_Default
FROM Sales.SalesOrderHeader
WHERE CustomerID = 11530
ORDER BY OrderDate,SalesOrderID;

RANGE running total

Explain : ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW

Means: Start from the first row and continue up to the current row.

SELECT
CustomerID,
OrderDate,
SalesOrderID,
TotalDue,
SUM(TotalDue) OVER ( PARTITION BY CustomerID ORDER BY OrderDate) AS RunningTotal_Default,
SUM(TotalDue) OVER ( PARTITION BY CustomerID ORDER BY OrderDate RANGE
BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS RunningTotal_RANGE
FROM Sales.SalesOrderHeader
WHERE CustomerID = 11530
ORDER BY OrderDate,SalesOrderID;

Adding two customer to show you with two

Running total with Rows

SELECT
CustomerID,
OrderDate,
SalesOrderID,
TotalDue,
SUM(TotalDue) OVER (PARTITION BY CustomerID ORDER BY OrderDate) AS RunningTotal_Default,
SUM(TotalDue) OVER (PARTITION BY CustomerID ORDER BY OrderDate RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS RunningTotal_RANGE,
SUM(TotalDue) OVER (PARTITION BY CustomerID ORDER BY OrderDate ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningTotal_ROWS
FROM Sales.SalesOrderHeader
WHERE CustomerID IN (11530,11000)
ORDER BY CustomerID,OrderDate,SalesOrderID;