0

I have data that looks like the following:

Group     Product    PercentAch
Gr1       Pr1        55%
Gr1       Pr2        65%
Gr2       Pr1        45%
Gr2       Pr2        95%
Gr3       Pr1        15%
Gr3       Pr2        35%
.
.
.

So basically the data describe a set of distinct products that are assigned to different groups. I am trying to create a query that will retrieve for me the top N% of records based on PercentAch by product by group. The Access help file says to sort by PercentAch in order to use the TOP property, but then that causes groups and products to be intermingled. My other option is to create a separate query for each Group/Product combination, which seems redundant. Is there a way to do this using just one (or two) queries?

5 Answers 5

3

You need to use a unique identifier otherwise if you have multiple products in the same group with the same PercentAch you will get all of those products ... ie more than the top 5% you wanted. Assume we have a unique ID on the Product. The SQL will be:

SELECT Group, ProductID, Product, PercentAch
FROM SalesPerformance
WHERE ProductID IN (
    SELECT TOP 5 PERCENT ProductID
    FROM SalesPerformance as S
    WHERE S.Group = SalesPerformance.Group
    ORDER BY PercentAch DESC
);
1

Here is your answer. Sorry but it took me awhile to get my mind around it. I knew I had seen this before:

ACC: How to Create a Top N Values per Group Query: http://support.microsoft.com/kb/153747

3
  • Thank you very much. I will look this over to figure out how to apply it to my situation. Subqueries still puzzle me. :-)
    – Jay
    May 28, 2009 at 12:39
  • I've been trying to figure out how this works for the past few hours now and I'm stuck. The greatest amount of success I've been able to achieve is retrieving the top N records by PercentAch using the subquery, which the top query then nicely groups. This is not what I'm trying to get to, and I'm kind of stuck as to why this is not working. How do I relate the subquery to the top query so that it's retrieving the top 5 records for each unique group/product combination? Sorry for my density.
    – Jay
    May 28, 2009 at 18:33
  • Jay, I've been trying to figure out this same question all day. That Microsoft article got it working for Top N per Group, but despite trying for hours with the subquery, I wasn't able to get Top N per Group/Product. I just figured it out though... Answer below. Sep 4, 2011 at 8:43
0

You need two queries. The Top query needs the output from the grouping query. If you try to put Top in the same query as Group, Top will act on the original source data, not the grouped data.

3
  • I'm sorry but I am not quite understanding. The first query will basically organize my data by group and product. I don't see how feeding this into the second query will solve my TOP problem, since I am still looking to get TOP N% by group and product.
    – Jay
    May 26, 2009 at 14:35
  • Is PercentAch calculated, or is it a field in a table? May 26, 2009 at 14:43
  • it is a field in a table. though ideally I'd like to do it either way.
    – Jay
    May 26, 2009 at 17:01
0

Jay, here it is:

SELECT Group, Product, PercentAch
FROM SalesPerformance
WHERE PercentAch IN (
    SELECT TOP 5 PERCENT PercentAch 
    FROM SalesPerformance as S
    WHERE S.Group = SalesPerformance.Group
    ORDER BY PercentAch DESC
);

Did you want the top 5 percent of records in each group, or just the top 5? If you just want the top 5 records in each group, remove the PERCENT keyword from the query.

3
  • Robert - Thank you for all your help so far. I've taken your above and implemented it without the PERCENT (for simplicity's sake). What I am getting are the top 5 PercentAch across all group/product combinations, not the top 5 PercentAch for each group/product.
    – Jay
    May 28, 2009 at 19:53
  • Jay, I fixed the query, see above. Because we are using the same table in the main query AND the subquery, the names had to be disambiguated. Also the subquery must compare groups, not products. May 28, 2009 at 20:38
  • Hi Robert I am drawing your attention to this question because you have three answers, which is a bit of a tangle.
    – Fionnuala
    Oct 31, 2012 at 12:12
0

I've been trying to figure out the exact same problem for most of the day. The answer it turns out was just to add Product to the subquery's WHERE clause.

SELECT Group, Product, PercentAch
FROM SalesPerformance
WHERE PercentAch IN (
    SELECT TOP 5 PERCENT PercentAch 
    FROM SalesPerformance as S
    WHERE S.Group = SalesPerformance.Group and S.Product = SalesPerformance.Product
    ORDER BY PercentAch DESC
);

This gave me the Top 5 % of PercentAch values for each Group/Product combination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.