473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Ranking by Subquery in Access

Hi

I have written a query in access to rank my scores, but I would like to rank these scores within a subset of all the scores. Here is my example

Class_ID Dog_Handler_ID Club Score Rank
1 12 Wallingford 32 4
1 13 Andover 95 8
1 14 Wallingford 40 5
2 12 Wallingford 93 7
2 14 Wallingford 21 3
3 12 Wallingford 92 6
3 13 Andover 14 2
3 14 Wallingford 14 2

What I would like is this

Class_ID Dog_Handler_ID Club Score Rank
1 12 Wallingford 32 1
1 13 Andover 95 3
1 14 Wallingford 40 2
2 12 Wallingford 93 2
2 14 Wallingford 21 1
3 12 Wallingford 92 3
3 13 Andover 14 1
3 14 Wallingford 14 1


If I could order by class_ID and then Rank that would be fab too.

My sql so far is

SELECT Res1.Class_ID, Res1.Dog_Handler_ID, Res1.Club, Res1.Score, (Select Count(*) from Q_Results Where [Score] < [Res1].[Score]+1;) AS Rank
FROM Q_Results AS Res1;

Many thanks
Mar 11 '10 #1

✓ answered by hedges98

Expand|Select|Wrap|Line Numbers
  1. SELECT Res1.Class_ID, Res1.Dog_Handler_ID, Res1.Club, Res1.Score, COUNT(*) as Rank
  2. FROM (
  3. SELECT Class_ID, Dog_Handler_ID, Club, Score
  4. FROM Q_Results
  5. GROUP BY Class_ID, Dog_Handler_ID, Club, Score) AS Res1
  6. INNER JOIN
  7. (
  8. SELECT Class_ID, Dog_Handler_ID, Club, Score
  9. FROM Q_Results
  10. GROUP BY Class_ID, Dog_Handler_ID, Club, Score
  11. ) AS b on Res1.Class_ID = b.Class_ID and Res1.Score >= b.Score
  12. GROUP BY Res1.Class_ID, Res1.Dog_Handler_ID, Res1.Club, Res1.Score
  13. ORDER BY Res1.Class_ID, Res1.Score, COUNT(*)
That should work but like the previous thread states, if two scores are the same then they will both be ranked 2 as opposed to 1.

Someone else might be able to offer a tidier solution though

11 2719
hedges98
109 100+
Wouldn't this work?
Expand|Select|Wrap|Line Numbers
  1. SELECT Res1.Class_ID, Res1.Dog_Handler_ID, Res1.Club, Res1.Score, (Select Count(*) from Q_Results Where [Score] < [Res1].[Score]+1;) AS Rank
  2. FROM Q_Results AS Res1
  3. ORDER BY Res1.Class_ID, Rank;
  4.  
Mar 11 '10 #2
no - when I use that query and run it I get a dialogue box asking to Enter Parameter Value Rank
Mar 11 '10 #3
hedges98
109 100+
Can you post your SQL for Q_Results please?

EDIT: Also, how come the Rank field of your example queries has different values in each query?

EDIT AGAIN: I think I follow, if the lowest score in each class is ranked highest?
Mar 11 '10 #4
SELECT Results.Class_ID, Results.Dog_Handler_ID, Handlers.Club, Results.Time, IIF([Results].[Faults]>0,[Results].[Faults],0) AS Faults, Results.Eliminated, Results.Obstacle, IIf([Eliminated]=Yes,100-[obstacle],[time]+[faults]) AS Score
FROM Handlers INNER JOIN (Dog_Handler INNER JOIN Results ON Dog_Handler.Dog_Handler_ID = Results.Dog_Handler_ID) ON Handlers.Handler_ID = Dog_Handler.Handler_ID
GROUP BY Results.Class_ID, Results.Dog_Handler_ID, Handlers.Club, Results.Time, Results.Faults, Results.Eliminated, Results.Obstacle, IIf([Eliminated]=Yes,100-[obstacle],[time]+[faults]);
Mar 11 '10 #5
hedges98
109 100+
I think I've confused myself. Can you explain this bit please
(Select Count(*) from Q_Results Where [Score] < [Res1].[Score]+1;)

Won't [Res1].[Score] be the same as [Score]?

EDIT: I just massively confused myself by trying to do two things at once so I'm going to leave this to someone who clearly knows what they are doing, ha!
Mar 11 '10 #6
Umm I am not sure - I have confused myself too as I have been trying things from various threads to get this to work

Thanks you for your help!
Mar 11 '10 #7
hedges98
109 100+
I don't feel quite so silly now, ha!

Does your query return anything at the moment? Or does it ask for a parameter?
Mar 11 '10 #8
My query ran and returned the first table in the first post until I added the following to the end of the query:

ORDER BY Res1.Class_ID, Rank;

Then it asked for the parameter value.

Before I opened this post I was trying to use the solution in the following thread, but couldn't work it out - http://bytes.com/topic/access/answer...g-subset-query.
Mar 11 '10 #9
hedges98
109 100+
Expand|Select|Wrap|Line Numbers
  1. SELECT Res1.Class_ID, Res1.Dog_Handler_ID, Res1.Club, Res1.Score, COUNT(*) as Rank
  2. FROM (
  3. SELECT Class_ID, Dog_Handler_ID, Club, Score
  4. FROM Q_Results
  5. GROUP BY Class_ID, Dog_Handler_ID, Club, Score) AS Res1
  6. INNER JOIN
  7. (
  8. SELECT Class_ID, Dog_Handler_ID, Club, Score
  9. FROM Q_Results
  10. GROUP BY Class_ID, Dog_Handler_ID, Club, Score
  11. ) AS b on Res1.Class_ID = b.Class_ID and Res1.Score >= b.Score
  12. GROUP BY Res1.Class_ID, Res1.Dog_Handler_ID, Res1.Club, Res1.Score
  13. ORDER BY Res1.Class_ID, Res1.Score, COUNT(*)
That should work but like the previous thread states, if two scores are the same then they will both be ranked 2 as opposed to 1.

Someone else might be able to offer a tidier solution though
Mar 11 '10 #10
Yay! that did it

Thank you sooooo much
Mar 11 '10 #11
hedges98
109 100+
Awesome! No worries pal, I'm glad I've given something back (even if the legwork was already done for me!) after all the help I've received on here myself
Mar 11 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: marcello stanley | last post by:
--Boundary_(ID_89yLhNmAybJBOhP/kmhVvw) Content-type: text/plain; charset=iso-8859-1 Content-transfer-encoding: 7BIT Hello, I am a mySQL newbie here and have some problem defining the mySQL...
3
by: Maarten | last post by:
I've the following problem: Table1 serie | dir Table2 serie | user I am making a webpage in php to administrate the values in the db. What I want is that I select all series...
8
by: Venkata C | last post by:
Hi! Does anyone here know of a way to goad DB2 into converting a correlated subquery to a non-correlated one? Does DB2 ever do such a conversion? We have a query of the form SELECT .. FROM A...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
1
by: Joseph Bloch | last post by:
In all the threads on ranking in Access queries, I've not seen anything that deals with ranking against a subset of the query results. What I need to do is take the following query results: Dept...
5
by: valglad | last post by:
Hi, The question below was posted about 4 years ago and noone was able to answer it back then. I have virtually the same type of problem so would appreciate if anyone can help. Thanks ...
8
by: AnndieMac | last post by:
I have an Access 2002 database of inventory count results, and I have been asked to create summaries of the items with the most losses at a store, region and national level. Individually, I have been...
6
by: keyur1719 | last post by:
Hi, I have a crosstab query which is based on a simple select query. Here is how the query works.. The base query gets it date from employee table and their incentives table for the given...
1
NeoPa
by: NeoPa | last post by:
Access QueryDefs Mis-save Subquery SQL Access stores its SQL for Subqueries in a strange manner :s It seems to replace the parentheses "()"with square brackets "" and (often) add an extraneous...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.