How to Use LAST_VALUE() SQL function

LAST_VALUE() Function

LAST_VALUE() function is introduced in SQL Server 2012. LAST_VALUE() returns first value from the list.

Syntax:- LAST_VALUE( [scalar_expression] )  OVER ( [ partition_by_clause ] order_by_clause rows_range_clause )

Example:-
NAME               MONTH    YEAR          AMOUNT
-------------------------------------------------
Alok Kumar Singh   MAY          2016        50000.48
Tomas Paul         MAY          2016        54000.43
Cristomfer Dee     MAY          2016        456463.89
Niel Macengi       MAY          2016        150021.00
Alok Kumar Singh   JUNE         2016        450000.00
Alok Kumar Singh   OCTOBER      2016        45.00


SELECT
   SAL.NAME,
   SAL.[MONTH],
   SAL.[YEAR],
   SAL.AMOUNT,
   LAST_VALUE(SAL.AMOUNT) OVER (ORDER BY SAL.AMOUNT ROWS BETWEEN 
UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) LstValue
FROM SALARY SAL


OUTPUT:-

NAME                MONTH   YEAR    AMOUNT    LstValue  
------------------------------------------------------------
Alok Kumar Singh   OCTOBER  2016    45.00       456463.89   
Alok Kumar Singh   MAY      2016    50000.48    456463.89   
Tomas Paul         MAY      2016    54000.43    456463.89   
Niel Macengi       MAY      2016    150021.00   456463.89   
Alok Kumar Singh   JUNE     2016    450000.00   456463.89   
Cristomfer Dee     MAY      2016    456463.89   456463.89   
How to Use LAST_VALUE() SQL function How to Use LAST_VALUE() SQL function Reviewed by Alok Singh on 02:55 Rating: 5
Powered by Blogger.