Hi,
A "TOP N By Group" kind of problem, such as finding the top 5
salesmen per country:
=================
SELECT a.country, a.salesman, a.TotalSales
FROM myTable As a
WHERE a.salesman = (SELECT TOP 5 b.salesman
FROM myTable As b
WHERE b.country=a.country
ORDER BY b.TotalSale DESC)
ORDER BY a.country, a.TotalSale DESC
==================
Another solution is to RANK BY GROUP, and keep those with a rank less or
equal to 5. A possible solution is:
=================
SELECT a.country, a.salesman, a.TotalSales, COUNT(b.country) As rank
FROM myTable As a LEFT JOIN myTable As b
ON a.country=b.country AND a.TotalSale < b.TotalSale
GROUP BY a.country, a.salesman, a.TotalSales
HAVING COUNT(b.country) <= 4
ORDER BY COUNT(b.country)
==================
where COUNT(b.country) returns the rank, within a given country, in this
context, but as a value from 0 to N-1, where N = number of salesmen in a
given country.
You can use other solution to compute the ranks, the idea is just to THEN
keep only the records with a rank <= a given fixed limit.
Hoping it may help,
Vanderghast, Access MVP