• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get top 5 result from database

 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, good day, assume i have 500 records , and the records is actually the data of customer who have make transaction in a company, my question is how to i decide who are the top 5 customers who having deal with highest transaction volume in company.. anyone have idea ?

thank you very much for your time !
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using oracle then RANK and DENSE_RANK will give you desired result.
 
java programer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If u r using MySQL

SELECT <column names> FROM <Table Name> ORDER BY <transation volume> DESC LIMIT 0,5;

Probably most databases will support this query
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for better and more accurate answer. you need to give the table structure mate.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Select * from (select * from transation order by tranAmount desc ) where rownum < 6;

May be some sort of query like this helps you.. Also it works only for oracle.
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for answering ..i using mySQL, so what is the best solution ? is it the one recommend by "java programmer" ? anyway, "java programmer" you have break the naming policy ..
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed "java programmer",

We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender
 
zhao weiwei
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you use SQL Server 2k
you should do "select top 5 * from table_name order by clounm_name desc"
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you will use DB2 in future just use FETCH FIRST 5 ROWS ONLY
 
Loren Rosen
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also need to think about whether you want to deal with ties in the ranking (though in this particular case I suppose it's unlikely that two customers will have exactly the same number of transactions)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic