Click here to Skip to main content
14,601,061 members
Rate this:
Please Sign up or sign in to vote.
See more:
how can i get nth height salary in sql server can anybody know help me......
Posted
Updated 25-Nov-16 22:02pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

Using Rank[^]
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100