GoodyGoody
Registered User.
- Local time
- Today, 08:27
- Joined
- Aug 31, 2019
- Messages
- 120
OK, so I'm going to try and explain this as best I can. Some of you will be a little familiar with the race timing database I am writing and have helped massively in getting it to where it is. A problem has just been highlighted when calculating the overall series' results. An athlete runs in 'n' races and those races can be in a series. Each series can have a number of races you are required to finish in order to be considered for a prize and they are the basis of your overall series score. I have a query which selects the raw results data and then another which selects only those athletes who have run the requisite number of races. Let's say there are 4 races in this series and you must have run 3. The next step is where the problem lies. Here's the SQL:
I have a runner whose results in his category (RaceCategoryPosition) are 6th, 6th, 7th and 7th. So, of course, as it runs through the underlying data (qrySeriesPositionOverallIndividual) it pulls in all 4 records (because both 6 and 7 are in the top 3 results). What I need to do is have a query which just selects the top 3 results for each athlete and if result 3 and 4 just happen to be the same who cares. Ideally I would just select the top 3 results for each athlete from qrySeriesPositionOverallIndividual without complicating the whole thing with this extra step but running TOP 3 on that query just gives me the absolute TOP 3 results which isn't what I want.
Can anyone help please?
Code:
SELECT *
FROM qrySeriesPositionOverallIndividual AS X
WHERE (((X.RaceCategoryPosition) In (SELECT TOP 3 RaceCategoryPosition FROM qrySeriesPositionOverallIndividual AS Y WHERE
Y.RaceRunner = X.RaceRunner
ORDER by Y.RaceCategoryPosition ASC)))
ORDER BY X.RaceRunner, X.RaceCategoryPosition;
I have a runner whose results in his category (RaceCategoryPosition) are 6th, 6th, 7th and 7th. So, of course, as it runs through the underlying data (qrySeriesPositionOverallIndividual) it pulls in all 4 records (because both 6 and 7 are in the top 3 results). What I need to do is have a query which just selects the top 3 results for each athlete and if result 3 and 4 just happen to be the same who cares. Ideally I would just select the top 3 results for each athlete from qrySeriesPositionOverallIndividual without complicating the whole thing with this extra step but running TOP 3 on that query just gives me the absolute TOP 3 results which isn't what I want.
Can anyone help please?