Tuesday 1 April 2014

Find the Nth Maximum and Minimum Value in a Column in SQL Server 2008

Hi friends,in this article i will explain about how to find the Nth Maximum and Minimum Value in a Column.
In previous articles i explained about Check if Database Exists In SQL Server – Different ways, Query to Get List of Tables in Database in SQL Server 2008 and Create ,list and Alter the trigger associated with a table with SQL Server?
I have the table data likee as shown in the below figure.

DECLARE @n INT
SET @n=2
SELECT USER_ID,username from
(


 SELECT USER_ID,username,ROW_NUMBER() OVER(ORDER BY USER_ID DESC) AS Higher
 FROM User_Details
) AS Higher
 WHERE Higher=@n

DECLARE @nth INT
SET @nth=2
SELECT USER_ID,username from
(
 SELECT USER_ID,username,ROW_NUMBER() OVER(ORDER BY USER_ID ASC) AS Lower
 FROM User_Details
) AS Lower
 WHERE Lower=@nth


Enter the value at @n what you have to enter then i will display according to @n value.


No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.