June 27, 2008 at 4:47 am
i am using sql server 2005 express edition...how to write a simple query to find the 4th or 3rd maximum value from a table..
i tried with 'limit'. but its not working....
note : pls dont give multiple subqueries...
help me..
June 27, 2008 at 4:53 am
try looking into RANK() or ROW_NUMBER() functions.....
Kev
June 27, 2008 at 5:20 am
If you post your table def, and some sample data i am sure there is a solution...
June 27, 2008 at 5:42 am
June 27, 2008 at 6:02 am
Hii,
There are different ways to get this..!
Try This...
----------------------------------------------------------
DECLARE @Emp Table ( ID int, Name Varchar(10), Salary int)
INSERT INTO @Emp
SELECT 1,'A',20000 UNION
SELECT 2,'B',25000 UNION
SELECT 3,'C',15000 UNION
SELECT 4,'D',18000 UNION
SELECT 5,'E',22000 UNION
SELECT 6,'F',24000
-- First Method
SELECT Min(Salary) FourthMax
FROM (SELECT TOP 4 * FROM @Emp ORDER BY Salary Desc) AS Temp
-- Second Method
SELECT Name,Salary
FROM (SELECT *,Row_Number() OVER (Order By Salary Desc) AS Row_Number FROM @Emp) as Temp
WHERE Row_Number = 4
If still you have some problem then feel free to ask... 😛
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply