tags:

views:

808

answers:

5

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?

A: 

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.

Robert Harvey
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.
Is PercentAch calculated, or is it a field in a table?
Robert Harvey
it is a field in a table. though ideally I'd like to do it either way.
A: 

Hi, Jay.

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

Robert Harvey
Thank you very much. I will look this over to figure out how to apply it to my situation. Subqueries still puzzle me. :-)
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.
A: 

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.

Robert Harvey
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, 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.
Robert Harvey
A: 

Robert - thank you. this change plus one other change produced exactly the results I was looking for. I bow before your superior access knowledge. Your patience is also much appreciated!

A: 

Hi Robert, are you aware regarding the plus change made by Jay? The query above does not work for each groups :-(

Daniele