Important alert: (current site time 7/15/2013 8:05:42 PM EDT)
 

VB icon

nth largest/smallest from a table

Email
Submitted on: 12/18/2001 12:22:58 AM
By: MuraliKrishna Vedagiri 
Level: Intermediate
User Rating: By 5 Users
Compatibility: SQL Server 7.0, SQL Server 6.5 and earlier, Informix , Oracle, Other
Views: 20467
 
     To find Nth largest and Smallest 'value'(Numbers) from a Table.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: nth largest/smallest from a table
-- Description:To find Nth largest and Smallest 'value'(Numbers) from a Table.
-- By: MuraliKrishna Vedagiri
--
-- Inputs:The N: The index of the largest/smallest number.
--
-- Returns:Largest / Smallest
--
-- Assumes:SImple SQL knowledge
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=374&lngWId=5--for details.--**************************************

select a.itemid from item a where (n-1) = (select count(*) from item b where b.itemid > a.itemid)
/** By changing > to < you can get Smallest Number **
** If you want to find the 4th largest, then 4-1 =3 in place of (n-1)/


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
12/18/2001 6:19:50 AMJames Travis

This is great, works as promised.
(If this comment was disrespectful, please report it.)

 
12/18/2001 9:28:45 AMkevin

why can't you just use...
SELECT TOP n [PERCENT] a, b FROM myTable
(If this comment was disrespectful, please report it.)

 
12/18/2001 9:51:29 AMSrinivas VM

Perfect and neat.
(If this comment was disrespectful, please report it.)

 
6/13/2002 7:29:28 AMSushil Daga

Wonderful. but will fail if there are multiple records with the same value, instead use:

SELECT a.itemid FROM item a WHERE (n-1) = (SELECT COUNT(distinct b.itemid) FROM item b WHERE b.itemid > a.itemid)

if no duplicate values then use this for better performance:

SELECT a.itemid FROM item a WHERE (n-1) = (SELECT COUNT('x') FROM item b WHERE b.itemid > a.itemid)

Please note: My solution will work perfectly on oracle.

(If this comment was disrespectful, please report it.)

 
6/27/2002 12:36:07 PMRoger

excelent
(If this comment was disrespectful, please report it.)

 
7/1/2002 4:44:51 PMvinnie

why not use ">=" in
"where b.itemid > a.itemid" so that you don't need to subtract 1 from n?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.