Here is the procedure to:
Using your sample data I created a table called Racing.
Then created the query RacingWithOdds
Code:
SELECT *
FROM (
SELECT a1.track
,a1.raceDate
,a1.RaceNumber
,a1.odds
FROM [Racing] AS a1
INNER JOIN [Racing] AS a2 ON (a1.track = a2.track)
AND (a1.racedate <= a2.racedate)
AND (a1.odds <= a2.odds)
AND a1.id <= a2.id
GROUP BY a1.track
,a1.racedate
,a1.racenumber
,a1.odds
,a1.id
) AS RankingQuery
ORDER BY RankingQuery.track
,RankingQuery.racedate
,RankingQuery.racenumber
,rankingquery.odds;
Then to get a repeatable structure that included Rank, based on ascending Odds within
the group Track, RaceDate, RaceNumber,
Using a Make Table query (CreateRacingWithOddsAndRank), created a new Table based on the RacingWithOdds query. Included a new field "Rank" in the new table which is called RacingWithOddsAndRank.
The query code is:
Code:
SELECT *
,Rank
INTO RacingWithOddsAndRank
FROM (
SELECT a1.track
,a1.raceDate
,a1.RaceNumber
,a1.odds
FROM Racing AS a1
INNER JOIN Racing AS a2 ON (a1.id <= a2.id)
AND (a1.odds <= a2.odds)
AND (a1.racedate <= a2.racedate)
AND (a1.track = a2.track)
GROUP BY a1.track
,a1.racedate
,a1.racenumber
,a1.odds
,a1.id
) AS RankingQuery
ORDER BY RankingQuery.track
,RankingQuery.racedate
,RankingQuery.racenumber
,RankingQuery.odds;
For my purposes I deleted the records that were created during the MakeTable query, and just kept the structure.
Then repopulated the table with the rank value based on the procedure following.
As mentioned above, the new table is called RacingWithOddsAndRank and it is populated with the data from RacingWithOdds and the Rank that was determined in the following procedure. This table has the Rank that can be used in subsequent processes.
Code:
'---------------------------------------------------------------------------------------
' Procedure : racingRank
' Author : mellon
' Date : 04/11/2015
' Purpose : Routine to use the RacingWithOdds query and to calculate the rank within
' the Track/RaceDate/RaceNumber groups. Then output the data with Rank to a new table
' called "RacingWithOddsAndRank".
'
' https://www.accessforums.net/queries/rank-within-group-query-56170.html
'---------------------------------------------------------------------------------------
'
Sub racingRank()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rs1 As DAO.Recordset
Dim Hold As String
Dim Rank As Integer
10 On Error GoTo racingRank_Error
20 Rank = 1
30 Set db = CurrentDb
40 Set rs = db.OpenRecordset("RacingWithOdds") 'based on query
50 Set rs1 = db.OpenRecordset("RacingWithOddsAndRank") ' create table with a rank field
60 With rs1
70 Do While Not rs.EOF 'Loop through the "RacingWithOdds" data
'check for eof
80 If rs.EOF Then GoTo CloseIT
'check if the group has changed
90 If rs!Track & rs!RaceDate & rs!Racenumber = Hold Then 'this is the group that detemines and resets rank
100 Rank = Rank + 1 'increment Rank
110 Else
120 Rank = 1 'reset rank to 1
130 Hold = rs!Track & rs!RaceDate & rs!Racenumber ' reset the group to be tested
140 End If
150 Debug.Print rs!Track & " " & rs!RaceDate & " " & rs!Racenumber & " " & rs!Odds & " " & Rank 'for debugging
'Write the record to RacingWithOddsAndRank
160 .AddNew
170 !Track = rs!Track
180 !RaceDate = rs!RaceDate
190 !Racenumber = rs!Racenumber
200 !Odds = rs!Odds
210 !Rank = Rank '<-------------------This adds rank to the group
220 .Update '<-------------------This updates the "RacingWithOddsAndRank" table
230 rs.MoveNext
240 Loop
250 End With
CloseIT:
260 Set rs = Nothing
270 Set rs1 = Nothing
280 db.Close
290 Debug.Print "Finished adding Rank " & Now()
300 On Error GoTo 0
310 Exit Sub
racingRank_Error:
320 MsgBox "Error " & Err.number & " at " & Erl & " (" & Err.Description & ") in procedure racingRank of Module AWF_Related"
End Sub
The data in the RacingWithOddsAndRank table looks like this:
track |
raceDate |
RaceNumber |
odds |
Rank |
BEL |
31/10/2015 |
1 |
1.5 |
1 |
BEL |
31/10/2015 |
1 |
2.5 |
2 |
BEL |
31/10/2015 |
1 |
4.5 |
3 |
BEL |
31/10/2015 |
1 |
9 |
4 |
BEL |
31/10/2015 |
2 |
1.5 |
1 |
BEL |
31/10/2015 |
2 |
6.5 |
2 |
BEL |
31/10/2015 |
2 |
8.5 |
3 |
BEL |
31/10/2015 |
2 |
9 |
4 |
SAR |
31/10/2015 |
1 |
1.5 |
1 |
SAR |
31/10/2015 |
1 |
2.5 |
2 |
SAR |
31/10/2015 |
1 |
2.5 |
3 |
SAR |
31/10/2015 |
1 |
9 |
4 |
SAR |
31/10/2015 |
2 |
1 |
1 |
SAR |
31/10/2015 |
2 |
1.5 |
2 |
SAR |
31/10/2015 |
2 |
11.5 |
3 |
SAR |
31/10/2015 |
2 |
12.5 |
4 |
SAR |
31/10/2015 |
3 |
0.5 |
1 |
SAR |
31/10/2015 |
3 |
1.5 |
2 |
SAR |
31/10/2015 |
3 |
11 |
3 |
SAR |
31/10/2015 |
3 |
11.5 |
4 |
SAR |
01/11/2015 |
1 |
1.45 |
1 |
SAR |
01/11/2015 |
1 |
1.45 |
2 |
SAR |
01/11/2015 |
1 |
9.9 |
3 |
SAR |
01/11/2015 |
1 |
11.45 |
4 |
I hope this is useful. I did put some comments within the post and the procedure to assist.
Ask, if there are questions.