MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 26th, 2004, 08:12 AM
MridulBuragohai MridulBuragohai is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 1 MridulBuragohai User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
nth highest value

we can extract the nth highest salary in the following way in sql server 2000.

say, sal is the field in emp table.

select top 1 q2.sal from
(select top "your N goes here" sal from
(select distinct(sal) from emp ) q1
order by sal desc) q2
order by q2.sal asc

put the value of N as 1 or 2 or 3 to get 1st highest, 2nd highest or 3rd highest value of salary respectively.

we use distinct key word, 'cause many people may have same salary. but the query is not modified for N value greater than the no. of records in the table. want a suggestion for that modification.

Mridul Buragohain.
Greatech Soft Solutions.
New Delhi, India

Reply With Quote
  #2  
Old October 30th, 2004, 11:14 PM
Username=NULL Username=NULL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: TX
Posts: 249 Username=NULL User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 42 sec
Reputation Power: 5
Send a message via Yahoo to Username=NULL
Geez, I've seen this question alot, and they always use the "salary" from an "employee" table

Anyway, I replied in the Oracle forum w/an approach that uses T-SQL, take a look. All you'd have to do is supply an if statement to first do a count(distinct salary) on the table, and compare it to the "nth" value desired, if "n" is < the count, then proceed, else throw an error of somekind.

http://forums.devshed.com/t187260/s...e1a915636d.html

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > nth highest value


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT