Question : How to limit query to top n records

I have a query that pulls the records of golfers who have a finishing position (field: 'Posn') of 20 or less on a specified course (field: 'coursename') over the last 5 years. The problem is that because people can be tied for a position, a query that sets Posn<21 will sometimes produce more than 20 results for that yield, and thus more than 100 results for the five years as a whole.

How can I set the query so that it returns no more than 20 records for each year and thus no more than 100 in total?

Here is the query:

SELECT DISTINCTROW Events.EventDate, Events.EventName, Course.Coursename, Tour.Tourname, Players.Surname, Players.Firstname, EventPlayerScore.Posn
FROM Tour INNER JOIN (Players INNER JOIN (((Course INNER JOIN Events ON Course.CourseId = Events.CourseID) INNER JOIN EventPlayerScore ON Events.EventID = EventPlayerScore.EventID) INNER JOIN EventPlayerStats ON Events.EventID = EventPlayerStats.EventID) ON (Players.PlayerID = EventPlayerStats.PlayerID) AND (Players.PlayerID = EventPlayerScore.PlayerID)) ON Tour.TourID = Events.TourID
WHERE (((Events.EventDate)>#1/1/2001#) AND ((Course.Coursename)="sAWGRASS") AND ((EventPlayerScore.Posn)<"21"))
ORDER BY Events.EventDate DESC , EventPlayerScore.Posn;

TIA.

Answer : How to limit query to top n records

scarlett3,

Echoing capricorn1:

SELECT TOP 20 DISTINCT Events.EventDate, Events.EventName, Course.Coursename, Tour.Tourname, Players.Surname, Players.Firstname, EventPlayerScore.Posn
FROM Tour INNER JOIN (Players INNER JOIN (((Course INNER JOIN Events ON Course.CourseId = Events.CourseID) INNER JOIN EventPlayerScore ON Events.EventID = EventPlayerScore.EventID) INNER JOIN EventPlayerStats ON Events.EventID = EventPlayerStats.EventID) ON (Players.PlayerID = EventPlayerStats.PlayerID) AND (Players.PlayerID = EventPlayerScore.PlayerID)) ON Tour.TourID = Events.TourID
WHERE (((Year(Events.EventDate)) = 2001) AND ((Course.Coursename)="sAWGRASS") AND ((EventPlayerScore.Posn)<"21"))
ORDER BY Events.EventDate DESC , EventPlayerScore.Posn
UNION
SELECT TOP 20 DISTINCT Events.EventDate, Events.EventName, Course.Coursename, Tour.Tourname, Players.Surname, Players.Firstname, EventPlayerScore.Posn
FROM Tour INNER JOIN (Players INNER JOIN (((Course INNER JOIN Events ON Course.CourseId = Events.CourseID) INNER JOIN EventPlayerScore ON Events.EventID = EventPlayerScore.EventID) INNER JOIN EventPlayerStats ON Events.EventID = EventPlayerStats.EventID) ON (Players.PlayerID = EventPlayerStats.PlayerID) AND (Players.PlayerID = EventPlayerScore.PlayerID)) ON Tour.TourID = Events.TourID
WHERE (((Year(Events.EventDate)) = 2002) AND ((Course.Coursename)="sAWGRASS") AND ((EventPlayerScore.Posn)<"21"))
ORDER BY Events.EventDate DESC , EventPlayerScore.Posn
UNION
SELECT TOP 20 DISTINCT Events.EventDate, Events.EventName, Course.Coursename, Tour.Tourname, Players.Surname, Players.Firstname, EventPlayerScore.Posn
FROM Tour INNER JOIN (Players INNER JOIN (((Course INNER JOIN Events ON Course.CourseId = Events.CourseID) INNER JOIN EventPlayerScore ON Events.EventID = EventPlayerScore.EventID) INNER JOIN EventPlayerStats ON Events.EventID = EventPlayerStats.EventID) ON (Players.PlayerID = EventPlayerStats.PlayerID) AND (Players.PlayerID = EventPlayerScore.PlayerID)) ON Tour.TourID = Events.TourID
WHERE (((Year(Events.EventDate)) = 2003) AND ((Course.Coursename)="sAWGRASS") AND ((EventPlayerScore.Posn)<"21"))
ORDER BY Events.EventDate DESC , EventPlayerScore.Posn
UNION
SELECT TOP 20 DISTINCT Events.EventDate, Events.EventName, Course.Coursename, Tour.Tourname, Players.Surname, Players.Firstname, EventPlayerScore.Posn
FROM Tour INNER JOIN (Players INNER JOIN (((Course INNER JOIN Events ON Course.CourseId = Events.CourseID) INNER JOIN EventPlayerScore ON Events.EventID = EventPlayerScore.EventID) INNER JOIN EventPlayerStats ON Events.EventID = EventPlayerStats.EventID) ON (Players.PlayerID = EventPlayerStats.PlayerID) AND (Players.PlayerID = EventPlayerScore.PlayerID)) ON Tour.TourID = Events.TourID
WHERE (((Year(Events.EventDate)) = 2004) AND ((Course.Coursename)="sAWGRASS") AND ((EventPlayerScore.Posn)<"21"))
ORDER BY Events.EventDate DESC , EventPlayerScore.Posn
UNION
SELECT TOP 20 DISTINCT Events.EventDate, Events.EventName, Course.Coursename, Tour.Tourname, Players.Surname, Players.Firstname, EventPlayerScore.Posn
FROM Tour INNER JOIN (Players INNER JOIN (((Course INNER JOIN Events ON Course.CourseId = Events.CourseID) INNER JOIN EventPlayerScore ON Events.EventID = EventPlayerScore.EventID) INNER JOIN EventPlayerStats ON Events.EventID = EventPlayerStats.EventID) ON (Players.PlayerID = EventPlayerStats.PlayerID) AND (Players.PlayerID = EventPlayerScore.PlayerID)) ON Tour.TourID = Events.TourID
WHERE (((Year(Events.EventDate)) = 2005) AND ((Course.Coursename)="sAWGRASS") AND ((EventPlayerScore.Posn)<"21"))
ORDER BY Events.EventDate DESC , EventPlayerScore.Posn

I still think you're better off handling this in Excel, though...

Regards,

Patrick
Random Solutions  
 
programming4us programming4us