Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 25th, 2005, 09:47 AM
Authorized User
 
Join Date: Sep 2003
Location: Delhi, Delhi, India.
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to prabodh_mishra
Default Fetching rows

Hi all,

Can anyone help me to fetch 4th or 5th highest value of a column in SQL Server 2000.

with Thanks
prabodh

__________________
Prabodh
 
Old September 25th, 2005, 10:32 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Code:
    SELECT MIN(T1.yourcolumn)
    FROM yourtable T1
        INNER JOIN yourtable T2 ON T1.yourcolumn <= T2.yourcolumn
    GROUP BY T1.yourcolumn
    HAVING count(*) <= 5
This returns the 5th highest value of your column.

I think.

The idea here is to take each value of <yourcolumn> and build a group consisting of a set of other <yourcolumn> values that are less than or equal to it. Those groups which have 5 or fewer values in them are the ones we want, and taking the minimum of those groups gives us the 5th highest value.

Take the MIN aggregate off, and you get a list of the 5 highest values - very handy when you want, say, the top 5 salaries, or whatever...

Since you posted this in the SQL Server forum, this could also be done with a subquery using the (nonstandard) TOP clause, as, e.g.
Code:
SELECT MAX(T1.yourcolumn)
FROM (SELECT TOP 5 yourcolumn FROM yourtable
        GROUP BY yourcolumn ORDER BY yourcolumn) T1
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old September 28th, 2005, 10:26 AM
Authorized User
 
Join Date: Sep 2003
Location: Delhi, Delhi, India.
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to prabodh_mishra
Default

Thanks Jeff.
:) It works perfectly...





Similar Threads
Thread Thread Starter Forum Replies Last Post
Fetching html from website nordboh ASP.NET 2.0 Professional 0 January 17th, 2007 02:40 PM
Fetching rows in dataset lily611 C# 4 April 27th, 2006 12:37 AM
Fetching filenames from a folder shazia1 VS.NET 2002/2003 1 January 26th, 2006 06:29 PM
Fetching a value in a SQL result... IP076 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 November 29th, 2004 02:26 PM
Fetching Newkid Classic ASP XML 2 October 22nd, 2004 03:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.