-----Original Message-----
Hi,
SELECT Name, Product
FROM somewhere As a
WHERE a.primaryKey IN ( SELECT TOP 25 b.primaryKey
FROM somewhere As b
WHERE b.name=a.name
ORDER BY b.ProductValue DESC, b.primaryKey )
ORDER BY Name, ProductValue DESC
The inner most query take the top 25 records (ordered by product value), for
a given name, a.name, and extract, from those records, their primarykey
value. The outer most query "loop" for each name, keep the records that
match something returned by the inner most query through the primary key,
and re-order the whole stuff.
Note that by default, Jet return the TOP N+equi, meaning that if there are
ex-equo in the breaking position, ie, for TOP 3 (ASCending values) in the
list ( 1, 2, 3, 3, 3, 3, 7, 8, 9), JET would return ( 1, 2, 3, 3, 3, 3) ,
ie, 6 values. Adding the primary key in the ORDER BY makes sure such
equi-ness won't occur and you will get only 25 records, at most. But if this
is unfair for a potential 26th which has the same value than the 25th,
remove the b.primaryKey in the order by clause or the innermost query:
SELECT Name, Product
FROM somewhere As a
WHERE a.primaryKey IN ( SELECT TOP 25 b.primaryKey
FROM somewhere As b
WHERE b.name=a.name
ORDER BY b.ProductValue DESC)
ORDER BY Name, ProductValue DESC
and you may get more than 25 records per name, but that comes from
equi-ness.
Hoping it may help,
Vanderghast, Access MVP
.