SQL LAST_VALUE


Example

The LAST_VALUE function provides the last value in an ordered result set, which you specify using a scalar expression.

SELECT TerritoryID, StartDate, BusinessentityID,
       LAST_VALUE(BusinessentityID) 
        OVER(ORDER BY TerritoryID) AS LastValue
FROM SalesTerritoryHistory;

This example uses the LAST_VALUE function to return the last value for each rowset in the ordered values.

TerritoryIDStartDateBusinessentityIDLastValue
12005-07-01 00.00.00.000280283
12006-11-01 00.00.00.000284283
12005-07-01 00.00.00.000283283
22007-01-01 00.00.00.000277275
22005-07-01 00.00.00.000275275
32007-01-01 00.00.00.000275277