FIRST_VALUE() function syntax with an example in SQL Server
Introduction:
In my previous posts we have seen what is Transact-Sql in sql server and What are new top features of T-Sql in server. This is one of the best sql server interview Questions and answers. Watch more ASP.NET, C#.NET, Jquery, Sql Server, HTML5, HTML, CSS, JQUERY UI Interview questions from here.
Description:
·
This function returns the first value in an ordered set of values
Example:
The following example uses FIRST_VALUE to return the name of the
product that is the least expensive in a given product category.
USE AdventureWorks2008R2;
GO
SELECT Name, ListPrice,
FIRST_VALUE (Name) OVER (ORDER BY
ListPrice ASC) AS LeastExpensive
FROM Production.Product
WHERE ProductSubcategoryID = 37;
Result:
Name
|
ListPrice
|
LeastExpensive
|
Patch Kit/8 Patches
|
2.29
|
Patch Kit/8 Patches
|
Road Tire Tube
|
3.99
|
Patch Kit/8 Patches
|
Touring Tire Tube
|
4.99
|
Patch Kit/8 Patches
|
Mountain Tire Tube
|
4.99
|
Patch Kit/8 Patches
|
LL Road Tire
|
21.49
|
Patch Kit/8 Patches
|
ML Road Tire
|
24.99
|
Patch Kit/8 Patches
|
LL Mountain Tire
|
24.99
|
Patch Kit/8 Patches
|
Touring Tire
|
28.99
|
Patch Kit/8 Patches
|
ML Mountain Tire
|
29.99
|
Patch Kit/8 Patches
|
HL Road Tire
|
32.60
|
Patch Kit/8 Patches
|
HL Mountain Tire
|
35.00
|
Patch Kit/8 Patches
|
If you have any queries or suggestions, please feel free to ask in comments section.
Post a Comment
Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.