473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

top 5 of group?

Hello:

tblDetails has the following fields:
AgencyCode, ProducerCode, AnnualPX
How can I create a query to show the Top 5 producers per agency with
the highest
Sum(PX)?

Oct 25 '06 #1
4 7632
I should point out that I am looking at top 5 per agency, not just 5
returned rows overall.

Thanks

BerkshireGuy wrote:
Hello:

tblDetails has the following fields:
AgencyCode, ProducerCode, AnnualPX
How can I create a query to show the Top 5 producers per agency with
the highest
Sum(PX)?
Oct 25 '06 #2
MP
"BerkshireGuy" <bd*****@yahoo.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
I should point out that I am looking at top 5 per agency, not just 5
returned rows overall.

Thanks

BerkshireGuy wrote:
Hello:

tblDetails has the following fields:
AgencyCode, ProducerCode, AnnualPX
How can I create a query to show the Top 5 producers per agency with
the highest
Sum(PX)?
Don't know if this will help
but heres what i found somewhere on jet.sql

The TOP keyword
The TOP keyword is used to return a certain number of rows that fall at the
top or bottom of a range that is specified by an ORDER BY clause. The ORDER
BY clause is used to sort the rows in either ascending or descending order.
If there are equal values present, the TOP keyword will return all rows that
have the equal value. Let's say that we want to determine the highest three
invoice amounts in our invoices database. We'd write a statement like this:

SELECT TOP 3 InvoiceDate, Amount
FROM tblInvoices
ORDER BY Amount DESC

We can also use the optional PERCENT keyword with the TOP keyword to return
a percentage of rows that fall at the top or bottom of a range that is
specified by an ORDER BY clause. The code looks like this:

SELECT TOP 25 PERCENT InvoiceDate, Amount
FROM tblInvoices
ORDER BY Amount DESC

Note that if you do not specify an ORDER BY clause, the TOP keyword will not
be helpful: it will return a random sampling of rows.

For more information about predicates, type all, distinct predicates in the
Office Assistant or on the Answer Wizard tab in the Microsoft Access Help
window, and then click Search.

hth
Mark
Oct 25 '06 #3
BerkshireGuy wrote:
I should point out that I am looking at top 5 per agency, not just 5
returned rows overall.

Thanks

BerkshireGuy wrote:
Hello:

tblDetails has the following fields:
AgencyCode, ProducerCode, AnnualPX
How can I create a query to show the Top 5 producers per agency with
the highest
Sum(PX)?
tblDetails
AgencyCode Long
ProducerCode Long
AnnualPX Double

qryRankPX:
SELECT AgencyCode, ProducerCode, (SELECT Count(A.ID) FROM tblDetails AS
A WHERE A.AnnualPX tblDetails.AnnualPX AND A.AgencyCode =
tblDetails.AgencyCode)+1 AS RankWithinGroup FROM tblDetails;

qryTopFive:
SELECT AgencyCode, ProducerCode, RankWithinGroup FROM qryRankPX WHERE
RankWithinGroup <=5;

or

qryTopFiveShowingTies:
SELECT AgencyCode, ProducerCode, RankWithinGroup & IIf((SELECT
Count(A.ProducerCode) FROM qryRankPX AS A WHERE A.AgencyCode =
qryRankPX.AgencyCode AND A.RankWithinGroup = qryRankPX.RankWithinGroup)
1, ' (tie)', '') AS AgencyRanking FROM qryRankPX WHERE RankWithinGroup <=5;
tblDetails
AgencyCode ProducerCode AnnualPX

1 1 1 9
2 1 2 9
3 1 3 9
4 1 4 8
5 1 5 8
6 1 6 8
7 1 7 4
8 1 8 2
9 1 9 2
10 1 10 2
11 1 11 2
12 1 12 2
13 1 13 2
14 2 14 12
15 2 15 12
16 2 16 11
17 2 17 10
18 2 18 9
19 2 19 9
20 2 20 4
21 2 21 4
22 2 22 4
23 2 23 4
24 2 24 4
25 2 25 4

!qryTopFiveShowingTies:
AgencyCode ProducerCode AgencyRanking
1 1 1 (tie)
1 2 1 (tie)
1 3 1 (tie)
1 4 4 (tie)
1 5 4 (tie)
1 6 4 (tie)
2 14 1 (tie)
2 15 1 (tie)
2 16 3
2 17 4
2 18 5 (tie)
2 19 5 (tie)

James A. Fortune
CD********@FortuneJames.com

Oct 25 '06 #4
"BerkshireGuy" <bd*****@yahoo.comwrote in
news:11**********************@e3g2000cwe.googlegro ups.com:
I should point out that I am looking at top 5 per agency, not just
5 returned rows overall.
You'll need to do a correlated subquery for that and the performance
will probably suck.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Oct 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
2
by: Tom Loach | last post by:
Our system administrator set up an NT server group in order to allow our users to login to our application via https to our sql server. The group appears as a User in SQL Server when you look at...
4
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the...
2
by: BillD | last post by:
I'm trying to derive a schema from a base schema. I want to redefine a "group" from the base schema in my derived schema in order to add more options to the "choice" aggregate (see schema1.xsd...
16
by: michael | last post by:
Is it possible to get all href URLs contained in a unordered list and place them in an array? Or in fact two different arrays, differently named one for each <ul> group? <ul> <li><a...
7
by: Darin | last post by:
I have a report that sub-totals on a group, then grand-totals at the report footer. If there's only one group, the sub-total and grand total are redundant, so I only want to show one of them. I...
1
by: David Horowitz | last post by:
Hi folks. I need to create a report that has a Group Header that pulls certain data from the Detail section. It's something like this: +--Report---------------------------------------- |...
7
by: Sameh Ahmed | last post by:
Hello there IsInrole gives ya the means to check if the current or impersonated user belongs to a specific windows role or group. is there a way to do the same without using ADSI to check if...
2
by: jon|k | last post by:
hi all-- i need to do a transformation that removes duplicates (among other things). to accomplish that, i'm trying to use for-each-group, but it doesn't work. i need to select for duplicates by...
3
by: Sebastian | last post by:
Hello all I have a report where I have two nested groups. I know there are only three standard options for running sum: None, Over Group and Over All. I have a MyTextBox in detail section where...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.