Skip to content

Instantly share code, notes, and snippets.

@beginor
Created December 1, 2014 06:32
Show Gist options
  • Select an option

  • Save beginor/259ff6b6bbcbbb57546f to your computer and use it in GitHub Desktop.

Select an option

Save beginor/259ff6b6bbcbbb57546f to your computer and use it in GitHub Desktop.
SQL Server 2012 TSQL New Function LAG, LEAD, FIRST_VALUE And LAST_VALUE
SELECT [Name]
,[Subject]
,[Score]
,LAG(Score, 1, 0) Over (Partition By Name Order By Subject) As [LagValue]
,LEAD(Score, 1, 0) Over (Partition By Name Order By Subject) As [LeadValue]
,FIRST_VALUE(Score) Over (Partition By Name Order By Subject) As [FirstValue]
,LAST_VALUE(Score) Over (Partition By Name Order By Subject) As [LastValue]
FROM [Test].[dbo].[Score]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment