SELECT custid, orderid, orderdate, val, SUM(val) OVER(PARTITION BY custid ORDER BY orderdate, orderid ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS runningtotal FROM Sales.OrderValues;
- 5. Grouping and Windowing
- from Training Kit (Exam 70-461): Querying Microsoft® SQL Server® 2012
- Publisher: Microsoft Press
- Released: December 2012
ORDER BY is ASC by default and since the window is between the current row (being processed) and the one preceding, it will generate a running total up to the most recent order.
Share this highlighthttp://www.safaribooksonline.com/a/training-kit-exam/61486/