T
Ted Allen
Hi David,
You should be able to do this by using a subquery to
calculate the group rank of each record, then set the
criteria for that field (such as <4 for the top three.
The following subquery placed as a calculated field in
your main query should calculate the group rank that you
are looking for (of course you would have to substitute
the actual table name):
GroupRank: (SELECT Count(*) FROM tblCompanyProfits AS VT
WHERE VT.Region = tblCompanyProfits.Region AND
VT.Profits > tblCompanyProfits.Profits)+1
The full sql would look like the following (again you
would have to substitute the table name).
SELECT tblCompanyProfits.Region,
tblCompanyProfits.Company, tblCompanyProfits.Profits,
(SELECT Count(*) FROM tblCompanyProfits AS VT WHERE
VT.Region = tblCompanyProfits.Region AND VT.Profits >
tblCompanyProfits.Profits)+1 AS GroupRank
FROM tblCompanyProfits
WHERE ((((SELECT Count(*) FROM tblCompanyProfits AS VT
WHERE VT.Region = tblCompanyProfits.Region AND
VT.Profits > tblCompanyProfits.Profits)+1)<4))
ORDER BY tblCompanyProfits.Region,
tblCompanyProfits.Profits DESC;
Note that if you enter the criteria < 4 for this field,
and there are multiple companies at rank three, it will
list all of them, so you could get a listing of more than
three companies per group. If you want to have a hard
limit of three, the subquery could be modified to use the
company name as a tiebreaker in those cases so that
companies with the same profit will receive different
ranks based on their company name. To do this, the
subquery criteria would be changed to count all less than
the current records profit OR all with the same profit
with a lesser company name. The first part would be the
same as the above, but the OR condition would have to be
added.
Post back if you do want to use the company name as the
tiebreaker and you need help with the syntax.
HTH, Ted Allen
the top n in the whole query). At the moment, the only
way I can figure out how to do it is to export to Excel,
number each row, and then paste back into Access (2002)
and restrict the query to ranks under n.
I'll settle for just being able to work with the top n of
each group. Example below if it helps.
You should be able to do this by using a subquery to
calculate the group rank of each record, then set the
criteria for that field (such as <4 for the top three.
The following subquery placed as a calculated field in
your main query should calculate the group rank that you
are looking for (of course you would have to substitute
the actual table name):
GroupRank: (SELECT Count(*) FROM tblCompanyProfits AS VT
WHERE VT.Region = tblCompanyProfits.Region AND
VT.Profits > tblCompanyProfits.Profits)+1
The full sql would look like the following (again you
would have to substitute the table name).
SELECT tblCompanyProfits.Region,
tblCompanyProfits.Company, tblCompanyProfits.Profits,
(SELECT Count(*) FROM tblCompanyProfits AS VT WHERE
VT.Region = tblCompanyProfits.Region AND VT.Profits >
tblCompanyProfits.Profits)+1 AS GroupRank
FROM tblCompanyProfits
WHERE ((((SELECT Count(*) FROM tblCompanyProfits AS VT
WHERE VT.Region = tblCompanyProfits.Region AND
VT.Profits > tblCompanyProfits.Profits)+1)<4))
ORDER BY tblCompanyProfits.Region,
tblCompanyProfits.Profits DESC;
Note that if you enter the criteria < 4 for this field,
and there are multiple companies at rank three, it will
list all of them, so you could get a listing of more than
three companies per group. If you want to have a hard
limit of three, the subquery could be modified to use the
company name as a tiebreaker in those cases so that
companies with the same profit will receive different
ranks based on their company name. To do this, the
subquery criteria would be changed to count all less than
the current records profit OR all with the same profit
with a lesser company name. The first part would be the
same as the above, but the OR condition would have to be
added.
Post back if you do want to use the company name as the
tiebreaker and you need help with the syntax.
HTH, Ted Allen
results from each group in an aggregate query (not just-----Original Message-----
I suspect there's no way to do this, but if anybody has an idea, I'd be much obliged.
I'm trying to get the top n (10, 5, 5%, whatever)
the top n in the whole query). At the moment, the only
way I can figure out how to do it is to export to Excel,
number each row, and then paste back into Access (2002)
and restrict the query to ranks under n.
by row, starting with 1 at the top of each group, butIn fact, ideally I'd like to rank each group in a query
I'll settle for just being able to work with the top n of
each group. Example below if it helps.
each as a subquery without going mad.Paste the following as a table, with the first line as field names:
Region Company Profits
US A 50
Europe V 15
US B 40
Europe W 14
US C 30
Europe X 13
US D 20
Europe Y 12
US E 10
Europe Z 10
I want a query that shows the three largest companies by profits in each region, i.e.:
Region Company Profits
Europe V 15
Europe W 14
Europe X 13
US A 55
US B 45
US C 35
Note that in real life I have too many Regions to do
much appreciated! Thanks in advance!As I say, I suspect I'm doomed, but any input would be