J
John
Hello.
I'm trying to figure out a query that will show me the top x% by group.
I have a query that counts the number of occurrences of a given event,
grouped by a store number. So, my results might look something like this:
Store Occurrences
1 6
2 105
3 3
4 35
5 5
6 258
7 167
8 43
9 42
10 810
11 3
12 28
13 109
14 204
15 5
16 5
17 3
18 3
19 6
20 3
First, I would like to have a query that returns, for example, the top
25%. I tried doing this by sorting the Occurrences field descending, and
then specifying the "top" property in the query as 25%. However, if I do
this, it will return the top 5 records (and any subsequent records that
are tied with the 5th record in the Occurrences field). So basically,
the results would be this:
Store Occurrences
10 810
6 258
14 204
7 167
13 109
However, I would like a different measure of the top 25%. If you add up
all the occurrences in the first data set above, you get a total of
1843. 25% of this number is 460.75, so what I want to show is the top x
number of stores whose occurrences add up to 460.75. Or, put another
way, if we start at the top of the results and work our way down,
keeping a running sum of the occurrences, how many stores do we go
through before we reach 460.75. In fact, this is the exact logic that
I've used in my current implementation, but it's all done with VBA code
iterating through a recordset and outputting to Excel - I would like it
all done in a query .In this example, I want the results to be:
Store Occurrences
10 810
The next piece of this is that I also need to be able to do this by
group. Each store is assigned to a region - maybe something like
"British Columbia" or "Greater Toronto Area". I would like the query to
show the top x% for each region.
Any thoughts?
I'm trying to figure out a query that will show me the top x% by group.
I have a query that counts the number of occurrences of a given event,
grouped by a store number. So, my results might look something like this:
Store Occurrences
1 6
2 105
3 3
4 35
5 5
6 258
7 167
8 43
9 42
10 810
11 3
12 28
13 109
14 204
15 5
16 5
17 3
18 3
19 6
20 3
First, I would like to have a query that returns, for example, the top
25%. I tried doing this by sorting the Occurrences field descending, and
then specifying the "top" property in the query as 25%. However, if I do
this, it will return the top 5 records (and any subsequent records that
are tied with the 5th record in the Occurrences field). So basically,
the results would be this:
Store Occurrences
10 810
6 258
14 204
7 167
13 109
However, I would like a different measure of the top 25%. If you add up
all the occurrences in the first data set above, you get a total of
1843. 25% of this number is 460.75, so what I want to show is the top x
number of stores whose occurrences add up to 460.75. Or, put another
way, if we start at the top of the results and work our way down,
keeping a running sum of the occurrences, how many stores do we go
through before we reach 460.75. In fact, this is the exact logic that
I've used in my current implementation, but it's all done with VBA code
iterating through a recordset and outputting to Excel - I would like it
all done in a query .In this example, I want the results to be:
Store Occurrences
10 810
The next piece of this is that I also need to be able to do this by
group. Each store is assigned to a region - maybe something like
"British Columbia" or "Greater Toronto Area". I would like the query to
show the top x% for each region.
Any thoughts?