SQL Functions (Analytic) LAST_VALUE

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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


Got any SQL Question?