Rank Query With Groups (Array?)

D

dmasch

Hello, I have a table of data with grades which are grouped by study. I need
to rank the grades relative to all the other grades in the same study. Below
is an example the study and grade fields with the desired outcome for the
rank.
Study Grade Rank
11111 89 1
11111 75 2
11111 65 3
22222 99 1
22222 87 2
Hopefully this can be done with simple SQL in a query and not with code but
I am open to anything that works. Many thanks in advance for your time and
expertise!
 
K

KARL DEWEY

Try this --
SELECT Study, Grade, (SELECT Count(*) FROM YourTable AS [XX] WHERE
YourTable.Study = [XX].Study AND YourTable.Grade <= [XX].Grade) AS Rank
FROM YourTable
ORDER BY Study, Grade DESC;
 
D

dmasch

Works perfectly. Thanks Karl!

KARL DEWEY said:
Try this --
SELECT Study, Grade, (SELECT Count(*) FROM YourTable AS [XX] WHERE
YourTable.Study = [XX].Study AND YourTable.Grade <= [XX].Grade) AS Rank
FROM YourTable
ORDER BY Study, Grade DESC;

--
Build a little, test a little.


dmasch said:
Hello, I have a table of data with grades which are grouped by study. I need
to rank the grades relative to all the other grades in the same study. Below
is an example the study and grade fields with the desired outcome for the
rank.
Study Grade Rank
11111 89 1
11111 75 2
11111 65 3
22222 99 1
22222 87 2
Hopefully this can be done with simple SQL in a query and not with code but
I am open to anything that works. Many thanks in advance for your time and
expertise!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top