473,441 Members | 1,757 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,441 software developers and data experts.

Ranking on a subset of a query

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 Subdept Amount
AAA A1 75
AAA A2 13
AAA A3 45
BBB B1 4
BBB B2 16
CCC C1 20
CCC C2 22
CCC C3 15

And rank the Subdept field _within_ the Dept field, so I would get the
following results:

Dept Subdept Amount Rank
AAA A1 75 1
AAA A2 13 3
AAA A3 45 2
BBB B1 4 2
BBB B2 16 1
CCC C1 20 2
CCC C2 22 1
CCC C3 15 3

I am completely stumped by this. Any help would be MOST appreciated!

Joe
Nov 13 '05 #1
1 7840
"Joseph Bloch" <jb****@surveyusa.com> wrote in message
news:df*************************@posting.google.co m...
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 Subdept Amount
AAA A1 75
AAA A2 13
AAA A3 45
BBB B1 4
BBB B2 16
CCC C1 20
CCC C2 22
CCC C3 15

And rank the Subdept field _within_ the Dept field, so I would get the
following results:

Dept Subdept Amount Rank
AAA A1 75 1
AAA A2 13 3
AAA A3 45 2
BBB B1 4 2
BBB B2 16 1
CCC C1 20 2
CCC C2 22 1
CCC C3 15 3


Here is one way:

select a.Dept, a.Subdept, a.Amount, count(*) as rank
from
(
select Dept, Subdept, Amount
from joe
group by Dept, Subdept, Amount
) as a
inner join
(
select Dept, Subdept, Amount
from joe
group by Dept, Subdept, Amount
) as b on a.Dept = b.Dept and a.Amount <= b.Amount
group by a.Dept, a.Subdept, a.Amount
order by a.Dept, a.Subdept, count(*)
You should be aware of how this handles ties, (if two rows tie for 1st place
it will give them each a ranking of 2). If that's not the behaviour you need
then you will have to modify the query or use a different method.





Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Irene | last post by:
Hi all again, Well, I have my Athletics database with Athletes, Competitions, Scores tables. I have a ranking query where I get back the list of the competitions-athletes and scores...
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...
0
by: Jeff Boes | last post by:
I hope this helps someone else ... I had struggled some time ago with attempts to get a rank of values query to work, but then I gave up and set it aside. I had another reason to attack it, and in...
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 ...
4
by: Jim | last post by:
I have a form that is using a query for the control source. The query put the data in order of TOP 20. I've added an extra textbox in the form and want to be able to automatically rank the records...
6
by: sara | last post by:
I hope someone can help with this. Our director wants to have a report that will have the departments (Retail stores) across the top, stores down the side and the RANKING of the YTD dept sales...
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...
5
by: Chris | last post by:
I was wodering if there was a way to rank numbers in a query like this #'s Rank 100 1 99 2 98 3 98 97 5 96 6 96
2
by: kevinlhamiltonsr | last post by:
INSERT INTO ( CC, HeldPayAccounts, ASG, , Command, OrderNo, , ) SELECT .CC, .HeldPayAccounts, .ASG, ., .Command, .OrderNo, . AS Expr2, (Select count(.) from as where ((. >= .) and (. = ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.