• marcia.j.wilson (12/9/2013)


    Any examples of how these would be used?

    Running totals. Traditionally done with slow set-based code, with a (relatively!) fast cursor, or with some dangerous unsupported side effect of a language feature.

    In SQL Server 2012, very simple:

    SELECT SalesPersonName,

    SaleDate,

    Amount,

    SUM(Amount) OVER (PARTITION BY SalesPersonName

    ORDER BY SaleDate

    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS RunningTotal

    FROM YourTable

    ORDER BY SalesPersonName, SaleDate;


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/