• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Returning top 10

 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Motivation example:
I have a table containing "persons". A person tuble is (id,name,value).
I need to return the 10 riches (as in, "most valueable") persons. Atm. im
using the following query:


Then I use a for(i=1;i<=10;i++) loop. I get the wanted result, but my problem is
that this will be pretty ineffective if I have 100000000 records in my DB.

Does anyone have any ideas or suggestions?

Please note, that I do not have any other tables in my DB. I thought about
making a table "rank" containing a "rank" and the value.. the problem is,
that with this solution I have to sort the table everytime I update a persons
value.

Edit: The persons value will be updated more often, than one wants to get
the top 10.

Thanks in advance.

/Svend Rost
[ May 02, 2005: Message edited by: Svend Rost ]
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

to those who might be interested you can get the top N records with the
following SQL queries:


The above works for Access and MS SQL Server

and

works for MySQL.

/Svend Rost
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In sybase ASE and ASA you can do the following:

 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!