MySQL Forums
Forum List  »  Newbie

Select top X rows for multiple key values
Posted by: rjl
Date: July 10, 2005 07:27PM

I have what I think is a simple problem but I aged brain is not functioning this evening....

I'm working on a (bike) race application that ranks racers by total points of races throughout the year. I have a table that looks like the following (for columns):

name
points
resultsKey

The 'resultsKey' field denotes the results of a particular race. This table is more of a log so 'names' appear more than once (once for each resultsKey). For example I'll have the following:

racer1 100 1
racer2 90 1
racer3 80 1
racer1 60 2
racer2 80 2
racer3 100 2
racer4 50 3
racer1 100 3
racer3 40 3
.....

The application needs to aggregate points for each racer, but only use the top X rows for each racer. In this example, if X == 2, then I need to have a query that returns the following rows:

racer1 100 1
racer1 100 2
racer2 90 1
racer2 80 2
racer3 100 2
racer3 80 1
racer4 50 3

In the end, the query(s) I run will create a summary table with one row/racer with the summary of points for their top X races. This would look like (using the racer table example above):

racer1 200
racer3 180
racer2 170
racer4 50

I can definitely do this in the application (using J2EE), but I would like to create the summary table in MySQL using some DB query(s). I'm running MySQL 4.1.11.

Thanks for any advice....

----
redBeard

Options: ReplyQuote


Subject
Written By
Posted
Select top X rows for multiple key values
rjl
July 10, 2005 07:27PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.