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
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](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjENpL8xR5eQamO4x56unlGavGu3gPP1lDCYYuvzCVICdndjY28NKK1_5nC2XLHzBEY41_xkztwqNYV0cuXgLs4VLyQchCKGm7t77JRBNpCukRyOGsZtT9ac_AJS1u3JG2WMf2s21yRLsY/s1600/first_value+function+example.png)
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](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjf_Hjjc5P8PdQjteTamNKDNZFs2O9oJNQDpqH6lsi4rA9yIaj-DFyCsa2pTATrUl9eR8Or3djQSYW3NGBfwEmXijuv2sSqS9GJlFZLb9ODhZHSLndDymLNSGGBqGH1hRdF5XQN7z80yYE/s1600/sql+server+2012+first_value+function.png)
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.