Chat with a LIVE Microsoft Access Expert!
 
       
 

         

   

Go Back   Access World Forums > Microsoft Access Discussion > Queries

 
 
Chat with a LIVE Microsoft Access Expert!
Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 07-17-2007, 07:03 AM
scottydel scottydel is offline
Registered User
 
Join Date: Apr 2007
Posts: 35
scottydel is on a distinguished road
help with aggregate query

Hello,

I have a table as such:

NAME RANK WIN_PERCENT DRAW_PERCENT

The table can have the same people in it multipe times, such as:

NAME LEVEL WIN_PERCENT DRAW_PERCENT

Bob 4 80% 4%
Bob 5 90% 5%
Bob 8 60% 3%

What I would like to do is return only the row in which Bob's Level is the Highest (MAX).

I wrote an aggregate query as such:

SELECT NAME, MAX(LEVEL) AS MAX_LEVEL, WIN_PERCENT, DRAW_PERCENT
FROM MYTable
GROUP BY NAME, WIN_PERCENT, DRAW_PERCENT

The problem is, as you'd expect, this query returns all rows, since I am grouping by WIN_PERCENT and DRAW_PERCENT as well.

Any ideas on what query to use to just return the row with the highest (MAX) LEVEL? i.e.

NAME LEVEL WIN_PERCENT DRAW_PERCENT

Bob 8 60% 3%

Thanks,

Scott
Reply With Quote
Sponsored Links
  #2  
Old 07-17-2007, 07:16 AM
Brianwarnock's Avatar
Brianwarnock Brianwarnock is offline
Retired
 
Join Date: Jun 2003
Location: Merseyside
Posts: 6,664
Brianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the rough
You tend to need 2 queries , one to find the max level and then one to join that back to the table to pull the rest of the data.

Brian
__________________
What is this life if, full of care,
We have no time to stand and stare
Reply With Quote
  #3  
Old 07-17-2007, 07:28 AM
ColinEssex's Avatar
ColinEssex ColinEssex is offline
Registered User
 
Join Date: Feb 2002
Location: Essex, UK
Posts: 6,837
ColinEssex is on a distinguished road
Or you could try this

Code:
SELECT tblName.names, Max(tblName.Level) AS MaxOfLevel, First(tblName.WinPercent) AS FirstOfWinPercent, First(tblName.Drawpercent) AS FirstOfDrawpercent
FROM tblName
GROUP BY tblName.names;
Firstly don't call your name field "Name" - it's a reserved word.

So in the query grid the first field is the name field aggregate "GroupBy"

Next field is Level - Totals line = Max

Then WinPercent and Drawpercent both Totals line set to "First"

I tried it with several names and repeats and it came up with one row for each name showing their MAX level

Col
__________________
"America is pregnant with promises and anticipation, but is murdered by the hand of the inevitable."
Reply With Quote
  #4  
Old 07-17-2007, 09:08 AM
Brianwarnock's Avatar
Brianwarnock Brianwarnock is offline
Retired
 
Join Date: Jun 2003
Location: Merseyside
Posts: 6,664
Brianwarnock is a jewel in the roughBrianwarnock is a jewel in the roughBrianwarnock is a jewel in the rough
Col using First returns the first entry entered in that field for that group not necessarily the one in the max record.

Brian
__________________
What is this life if, full of care,
We have no time to stand and stare
Reply With Quote
  #5  
Old 07-17-2007, 03:11 PM
ColinEssex's Avatar
ColinEssex ColinEssex is offline
Registered User
 
Join Date: Feb 2002
Location: Essex, UK
Posts: 6,837
ColinEssex is on a distinguished road
Quote:
Originally Posted by Brianwarnock View Post
Col using First returns the first entry entered in that field for that group not necessarily the one in the max record.

Brian
Not if you have the 2nd field as 'level' set to Max. I tried it using 6 different names each with several entries and it always pulled up the Max level for each name

Col
__________________
"America is pregnant with promises and anticipation, but is murdered by the hand of the inevitable."
Reply With Quote
  #6  
Old 07-17-2007, 03:20 PM
pbaldy's Avatar
pbaldy pbaldy is offline
Who is John Galt?
 
Join Date: Aug 2003
Location: Nevada, USA
Posts: 11,408
pbaldy is a glorious beacon of lightpbaldy is a glorious beacon of lightpbaldy is a glorious beacon of lightpbaldy is a glorious beacon of lightpbaldy is a glorious beacon of light
I'm with Brian. First will not reliably return the values from the same record as the one with the max level. You need the 2 query method.
__________________
Paul
Microsoft Access MVP
www.BaldyWeb.com
Reply With Quote
  #7  
Old 07-18-2007, 12:58 AM
ColinEssex's Avatar
ColinEssex ColinEssex is offline
Registered User
 
Join Date: Feb 2002
Location: Essex, UK
Posts: 6,837
ColinEssex is on a distinguished road
Quote:
Originally Posted by pbaldy View Post
I'm with Brian. First will not reliably return the values from the same record as the one with the max level. You need the 2 query method.

Ok, maybe I just got lucky when it pulled up all the correct records - go with Brian's method

Col
__________________
"America is pregnant with promises and anticipation, but is murdered by the hand of the inevitable."
Reply With Quote
Sponsored Links
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Query using variable Don White Queries 1 07-04-2005 06:23 AM
How do I use DAO to compare text box on form to query result? KyleB Modules & VBA 9 09-30-2004 02:19 PM
Question about the crosstab query and a query keawee Queries 2 07-24-2003 10:06 PM
CrossTab Query...Aggregate Function Rich_Lovina Queries 0 10-28-2001 06:06 PM
Help returning blank (no entry) records with a parameter query rgsmpx Queries 2 10-09-2001 10:04 AM


All times are GMT -8. The time now is 07:22 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
(c) copyright 2009 Access World