>
... when i run it nothing happens and i dont know why
<
I think Access was running the query. It took time to run a subquery against a fairly large table. Proper indexing might help.
Since there are duplicate Times or Distances in some "Ag Sex Discipline" groups, there is bound to be a tie in some groups. When this happens, using Count(*) Between 1 And 5 won't work because the count number for those records in a tie may become greater than 5. Besides, when you use r_time >= a.r_time in the subquery, you are actually looking for the longest rather than the shortest times.
Instead you can use Top 5 in the subquery. And you don't need to concatenate any fields.
As the times and the distances require two different criteria, you can do it with two SQL statements and Union them together as in the first query in the attached DB:
SELECT a.*
FROM [_08072003bu completeresults] AS a
WHERE [R_Time] In (Select Top 5 R_Time from [_08072003bu completeresults] where Ag=a.Ag and Sex=a.Sex and Discipline=a.Discipline order by R_Time)
UNION
SELECT b.*
FROM [_08072003bu completeresults] AS b
WHERE [R_Distance] In (Select Top 5 R_Distance from [_08072003bu completeresults] where Ag=b.Ag and Sex=b.Sex and Discipline=b.Discipline order by R_Distance Desc)
ORDER BY Ag, Sex, Discipline;
It seems Access doesn't allow formatting fields in a Union query in Design view. I have used a second query and formatted the Time and the Distance fields in Design view. So you can open the attached DB and run the second query.
Notes.
-- In order to meet the file size limit here, I have deleted some fields from your table and reduced the number of records to 3000.
-- As running subqueries takes time, I have added an AgSexDiscipline index to the table on the three fields: Ag,Sex,Discipline to speed up performance. On my system, running the second query against the 3000 records without the index took about 1½ minutes. After indexing, it took less then 3 seconds.
-- Where there is a tie in a group, more than 5 records are returned e.g.
In SEN Male Discus, 6 records are returned.