Is it possible to get last but one value of sorted variable using SQL function FIRST_VALUE
? Here is a way to get the last value.
select
[MyVal_Last] = FIRST_VALUE([MyValue]) OVER (PARTITION BY [Category] ORDER BY [MyValue] DESC)
from tbl
Is it possible to modify it to get second last value?
Update. I am familiar with different ways of getting the last but one value mentioned i.e. here Those answers are old and have grown long beards. I wonder if new version of SQL Server can do it in a more elegant way, using new functions.