Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

FIRST_VALUE function in SQL Server

Suggested Videos
Part 112 - Calculate running total in SQL Server 2012
Part 113 - NTILE function in SQL Server
Part 114 - Lead and Lag functions in SQL Server 2012



In this video we will discuss FIRST_VALUE function in SQL Server



FIRST_VALUE function 
  • Introduced in SQL Server 2012
  • Retrieves the first value from the specified column
  • ORDER BY clause is required
  • PARTITION BY clause is optional
Syntax : FIRST_VALUE(Column_Name) OVER (ORDER BY Col1, Col2, ...)

FIRST_VALUE function example WITHOUT partitions : In the following example, FIRST_VALUE function returns the name of the lowest paid employee from the entire table.

SELECT Name, Gender, Salary,
FIRST_VALUE(Name) OVER (ORDER BY Salary) AS FirstValue
FROM Employees

first_value function example

FIRST_VALUE function example WITH partitions : In the following example, FIRST_VALUE function returns the name of the lowest paid employee from the respective partition.

SELECT Name, Gender, Salary,
FIRST_VALUE(Name) OVER (PARTITION BY Gender ORDER BY Salary) AS FirstValue
FROM Employees

sql server 2012 first_value function

No comments:

Post a Comment

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.