Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how can i get nth height salary in sql server can anybody know help me......
Posted
Updated 25-Nov-16 22:02pm

Using Rank[^]
 
Share this answer
 
Comments
Sandeep Mewara 24-Jan-11 7:47am    
How does RANK help here?

It should be a simple nested query with a TOP. Right?
Goutam Patra 24-Jan-11 7:58am    
Like
SELECT * FROM (
SELECT CODE, AMOUNT AS AMT,
RANK() OVER (PARTITION BY CODE ORDER BY AMOUNT) AS MYRANK
FROM CBJDETAIL
) AS A
WHERE MYRANK = 4

Sandeep Mewara 24-Jan-11 8:01am    
But, how does this give me Nth highest salary?

I want the 7th highest salary with employee names may be... RANK does not do anything related to that. Data is stored in any order.
Goutam Patra 24-Jan-11 8:08am    
Simple,
CREATE TABLE SALARY (EMPCODE VARCHAR(10), SALARY MONEY)
INSERT INTO SALARY VALUES('A1', 15000)
INSERT INTO SALARY VALUES('A2', 20000)
INSERT INTO SALARY VALUES('A3', 25000)
INSERT INTO SALARY VALUES('A4', 30000)
INSERT INTO SALARY VALUES('A5', 35000)

SELECT * FROM (
SELECT EMPCODE, SALARY,
RANK() OVER (ORDER BY SALARY DESC) AS MYRANK
FROM SALARY
) AS A
WHERE MYRANK = 4

This will fetch 4th highest salary.
wont that. Please reply if it does.
Sandeep Mewara 24-Jan-11 8:11am    
Hmm, interesting. Will surely look into it working form and update you. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900