T-SQL Tutorial

T-SQL LAST_VALUE


The T-SQL LAST_VALUE function is an analytic function and is used in SQL Server database to return the last value in an ordered set of values.

LAST_VALUE Syntax


LAST_VALUE ( [ scalar_expression ] ) [ IGNORE NULLS | RESPECT NULLS ]
OVER ( [ partition_by_clause ] order_by_clause rows_range_clause )



LAST_VALUE Example


select b.*,
FIRST_VALUE(b.name) OVER (ORDER BY b.price) AS FirstValue,
LAST_VALUE(b.name) OVER (ORDER BY b.price) AS LastValue
from books b;