2

I have results like this

enter image description here

I need to get the latest race of each jockey (First getting the max date of the Race, and then the Max Race number). The results should look something like this:

enter image description here

Note: The database is very huge. The query should be very fast.

Rick James
  • 135,179
  • 13
  • 127
  • 222
Dragon Warrior
  • 307
  • 3
  • 16
  • Don't forget to put an index on the Jockey column if you use it frequently to retrieve data. – Ananta Jan 19 '18 at 15:12
  • For efficient "groupwise max", including getting more than one: http://mysql.rjweb.org/doc.php/groupwise_max – Rick James Jan 19 '18 at 16:49

2 Answers2

2

When you say "Max Race number", do you mean the number of the latest race (race with the most recent date), or the highest number?

Assuming you mean the former, and making up the name 'Races' for your table:

SELECT 
    Race.*
FROM 
    (
        SELECT Jockey, MAX(Date) as MaxDate 
        FROM Races GROUP BY Jockey
    ) MaxRaceDate 
    inner join (
        SELECT Jockey, Date, MAX(Race) as MaxRace
        FROM Races 
        GROUP BY Jockey, Date
    ) MaxRaceForDate 
        on MaxRateDate.Jockey = MaxRaceForDate.Jockey
            and MaxRateDate.MaxDate = MaxRaceForDate.Date
    inner join Race
        on Race.Jockey=MaxRace.Jockey 
            and Race.Date = MaxRace.MaxDate
            and Race.Race = MaxRaceForDate.MaxRace

This query is getting long, so it's possible that I've got an error, but it should be roughly right. ;)

Basically, you need an aggregate query on the table to find the date, then another aggregate query for the maximum rate for the date, then finally the table itself to get the information. And then you need to put them together with either subqueries or joins, and joins are typically much faster, so I went that way.

Geoffrey Wiseman
  • 5,459
  • 3
  • 34
  • 52
  • Question has been edited and commented to suggest that the former interpretation is correct. – Geoffrey Wiseman Jan 19 '18 at 15:22
  • A jockey can ride on two races on the same date, so I want to check the race number also – Dragon Warrior Jan 19 '18 at 15:30
  • Yes, the above query will give the results, but it will take quite long to get the results, is there any other way to get the results quickly? – Dragon Warrior Jan 19 '18 at 15:48
  • You'd probably have to profile the query, see what's taking the time. Adding indexes on jockey, race and date would be an obvious first start. If you can't change the database, then I'm not sure there's much you're going to be able to do to speed things up. – Geoffrey Wiseman Jan 19 '18 at 16:56
1

I believe, you looking for:

SELECT Jockey,MAX(DATE),MAX(RACE) FROM myTable  GROUP BY Jockey
apomene
  • 14,282
  • 9
  • 46
  • 72