If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Finding the n.th higest value

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-13-04, 11:17
sberg43 sberg43 is offline
Registered User
 
Join Date: Feb 2004
Posts: 2
Finding the n.th higest value

Hi.

I have a table, "Message", that contains information about several messages. On field in the table, "no_views" shows how many times a message has been viewed.

I want to be able to show the top three messages with the most views. How can this be done in sql?

A query against a table like this
MessageID | No_Views
1 | 10
2 | 110
3 | 200
4 | 150
5 | 5

should produce this output:
3 | 200
4 | 150
2 | 110

(The order is not important).

Thanks for your help!
Reply With Quote
  #2 (permalink)  
Old 02-13-04, 11:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
select MessageID
     , No_Views
  from Message M
 where 3 > (
   select count(*)
     from Message 
     where No_Views > M.No_Views )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-13-04, 11:57
sberg43 sberg43 is offline
Registered User
 
Join Date: Feb 2004
Posts: 2
Perfect!

Thanks for a very smart solution :-)
Reply With Quote
  #4 (permalink)  
Old 02-17-04, 04:29
Sowmyam Sowmyam is offline
Registered User
 
Join Date: Feb 2004
Posts: 41
Rudy,
ok this is for n maximum values, if i wnat to print only nth max, say if i want 1st, 2nd , 3rd,.. max value.

how to do it in sql.
__________________
'A candle will loose nothing by lighting an another candle'
Reply With Quote
  #5 (permalink)  
Old 02-17-04, 08:00
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
if you want only the nth entry, use n-1 in the query

for example, to find the 13th highest entry,
Code:
select MessageID
     , No_Views
  from Message M
 where 12 = (
   select count(*)
     from Message 
     where No_Views > M.No_Views )
in english, the query finds the row where there are 12 rows with a higher value, so that must be the row with the 13th highest value
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 02-17-04, 08:12
Sowmyam Sowmyam is offline
Registered User
 
Join Date: Feb 2004
Posts: 41
Ok. Thank u. Actully i tried it out and got the solution.
__________________
'A candle will loose nothing by lighting an another candle'
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On