I'm attempting to create an SQL SELECT statement that is calculating a value on the fly which I need to also RANK. The SELECT statement uses an INNER JOIN which is complicating matters. To make things more difficult I am having to do this against a MS Access Database of which I am not able to change the structure.
The tables are as follows:
Results
=======
CompID
PlayerNumber
GrossTotal
Players
=======
PlayerNumber
PlayerName
PlayerHandicap
I'm needing to calculate the Net Score by subtracting PlayerHandicap from GrossTotal and then would like to rank them by Net Score.
Currently I have thew following to give me the net score:
SELECT Results.CompNumber, Results.PlayerNumber, Results.GrossTotal, Players.Name, Players.Handicap, Results.GrossTotal-Results.Handicap AS Net
FROM Results INNER JOIN
Players
ON Results.PlayerNumber = Players.PlayerNumber
WHERE Results.CompNumber=25;
To achieve ranking I use the following addition (for Gross Scores) in a different query:
SELECT Results.CompNumber, Results.PlayerNumber, Results.GrossTotal, Players.Name, Players.Handicap,
(SELECT COUNT([Primary]) As HowMany
FROM Results As Dupe
WHERE (Dupe.GrossTotal < Results.GrossTotal) AND CompNumber=25
)+1 AS Rank
FROM Players INNER JOIN
Results
ON Players.Primary = Results.PlayerNumber
WHERE Results.CompNumber=25));
I'm not sure how to combine the RANK part of the latter statement with the Net score calculation.