Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 reg:- select

Author  Topic 

esambath
Yak Posting Veteran

89 Posts

Posted - 2010-02-03 : 08:34:28
hi,

i have one table salary

empid empname salary
1 A 23000
2 B 30000
3 C 22000

get record in 2nd highest salary

output

1 A 23000

kindly help this one

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2010-02-03 : 08:36:37
SELECT empid, empname, salary
FROM table
WHERE (RANK() OVER PARTITION BY salary DESC)) = 2

...I'm not too good at this on the fly but I think it should work...

- Lumbago
If the facts don't fit the theory, change the facts. Albert Einstein
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-03 : 08:58:17
Here are some more methods
http://beyondrelational.com/blogs/madhivanan/archive/2007/08/27/find-nth-maximum-value.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-03 : 11:07:28
quote:
Originally posted by Lumbago

SELECT empid, empname, salary
FROM table
WHERE (DENSE_RANK() OVER PARTITION BY salary DESC)) = 2

...I'm not too good at this on the fly but I think it should work...

- Lumbago
If the facts don't fit the theory, change the facts. Albert Einstein


might have a problem when you've two records with same highest salary so I would put my bet on small modification above

Go to Top of Page
   

- Advertisement -