Answered by:
Problem with Rank in Access Query

Question
-
I think my Access skills and SQL skills are ok, but my Ranking skills are not on par. I have an image that looks like this.
This is a very simple calculation. Material = Level * Sever. I'm now trying to Rank order the 'Material' calculated field. Any suggestions as to how to do this? I've done some Google searching and tried a couple of things. I keep getting '1' for everything. That's definitely not right. I'd like to do some kind of count, and add 1 to the previous record and sort by the 'Material' field. How can I do that?
Check out my book!!!
- Edited by ryguy72 Monday, April 4, 2016 3:44 PM
Monday, April 4, 2016 3:40 PM
Answers
-
Try this:
SELECT T1.Level, T1.Sever, T1.Level*T1.Sever AS Material,
(SELECT COUNT(*)+1
FROM YourTable AS T2
WHERE T2.Level*T2.Sever > T1.Level*T1.Sever) AS Ranking
FROM YourTable AS T1
ORDER BY T1.Level*T1.Sever DESC;Ken Sheridan, Stafford, England
- Marked as answer by ryguy72 Tuesday, April 5, 2016 2:14 PM
Monday, April 4, 2016 7:18 PM -
See Rownumbering.zip in my public databases folder at:
https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169
Note that if you are using an earlier version of Access you might find that the colour of some form objects such as buttons shows incorrectly and you will need to amend the form design accordingly.
If you have difficulty opening the link copy its text (NB, not the link location) and paste it into your browser's address bar.
This little demo file includes examples of various methodologies for both ranking and
numbering of a query's result table. To break ties the key is brought into play. The simplest way to number rows is of course to use the RunningSum property of an unbound text box control with a ControlSource property of =1.Ken Sheridan, Stafford, England
- Marked as answer by ryguy72 Tuesday, April 5, 2016 2:14 PM
Tuesday, April 5, 2016 8:25 AM
All replies
-
Try this:
SELECT T1.Level, T1.Sever, T1.Level*T1.Sever AS Material,
(SELECT COUNT(*)+1
FROM YourTable AS T2
WHERE T2.Level*T2.Sever > T1.Level*T1.Sever) AS Ranking
FROM YourTable AS T1
ORDER BY T1.Level*T1.Sever DESC;Ken Sheridan, Stafford, England
- Marked as answer by ryguy72 Tuesday, April 5, 2016 2:14 PM
Monday, April 4, 2016 7:18 PM -
Thanks Ken! That works pretty good, but if there is a tie, there is a problem. As you can see in the image, the rank numbers stay the same but the counts are always increasing.
This is called dense ranking. Is there an easy way to make it ordinal ranking? This may, literally, be a row count type of thing. I tried to count the rows, but couldn't get anything working. It's super-simple in SQL Server, but apparently quite a feat in Access.
Any ideas?
Monday, April 4, 2016 10:12 PM -
See Rownumbering.zip in my public databases folder at:
https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169
Note that if you are using an earlier version of Access you might find that the colour of some form objects such as buttons shows incorrectly and you will need to amend the form design accordingly.
If you have difficulty opening the link copy its text (NB, not the link location) and paste it into your browser's address bar.
This little demo file includes examples of various methodologies for both ranking and
numbering of a query's result table. To break ties the key is brought into play. The simplest way to number rows is of course to use the RunningSum property of an unbound text box control with a ControlSource property of =1.Ken Sheridan, Stafford, England
- Marked as answer by ryguy72 Tuesday, April 5, 2016 2:14 PM
Tuesday, April 5, 2016 8:25 AM -
Tuesday, April 5, 2016 3:32 PM