Top Values in sub groups

ConfusedMan25

Registered User.
Local time
Today, 14:06
Joined
Jul 14, 2008
Messages
10
I am no expert when it comes to VBA or SQL codes, therefore, the solution of my request may be lengthy, but it needs to be simplistic. What I am trying to do is to list out the top 2 list of my sorting criteria in a sub-group. For example, how do I list out the top 2 players with the top batting average of a given team in a division? The desired result should look something like….the data may not reflect reality, of course.

Yankees Royals Redsox
Jeter DeJesus Drew
Rodriquez Pena Damon

I don’t mind making multiple queries to get the desire result, but I don’t follow VBA or SQL well.
 
Here's a sample query that you can work with in the Northwind database.

It'll return the top three products (based on UnitsInStock) for each category. You should be able to adapt to your table structure(s).

Code:
SELECT Categories.CategoryName AS Expr1, Products.ProductName, Products.UnitsInStock
FROM Categories LEFT JOIN Products ON Categories.CategoryID = Products.CategoryID
WHERE (((Products.UnitsInStock) In (SELECT
    Top 3 [UnitsInStock] 
FROM
   Products 
WHERE
   [CategoryID]=[Categories].[CategoryID] 
ORDER BY
   [UnitsInStock] Desc)))
ORDER BY Categories.CategoryName, Products.UnitsInStock DESC;

To test, copy to a new query in Northwind.

HTH - Bob
 

Users who are viewing this thread

Back
Top Bottom