LAST VALUE

suggest change

LAST_VALUE returns a value from the end of the window frame, not automatically from the end of the partition. The default frame stops at the current row, so the query below does not reliably return the last row of each territory. Add ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING for that. ORDER BY TerritoryID also has ties, so which row is last is not defined. The sample output is not a guaranteed result of this query.

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.

TerritoryID | StartDate |BusinessentityID|LastValue|
—— | —— |—–|––|
1|2005-07-01 00.00.00.000 |280|283|
1|2006-11-01 00.00.00.000 |284|283|
1|2005-07-01 00.00.00.000 |283|283|
2|2007-01-01 00.00.00.000 |277|275|
2|2005-07-01 00.00.00.000 |275|275|
3|2007-01-01 00.00.00.000 |275|277|

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents