In Foursquare, the user who has the highest score for a place in the last N days is awarded the Mayorship of that place.
What is the most efficient way to implement that?
A user could have checked into hundreds of places. To display all the mayorships that belong to a user, it'd be necessary to go through all those hundreds of places one by one and check if he has the highest score in the last 60 days for each place-- that sounds very inefficient.
Is there any SQL or algorithmic magic that could perform the task quickly?
UPDATE: I'm using MySQL and Django