| Chat with a LIVE Microsoft
Access Expert! |
||||
|
||||
|
#1
|
|||
|
|||
|
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 |
| Sponsored Links |
|
#2
|
||||
|
||||
|
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 |
|
#3
|
||||
|
||||
|
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; 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." |
|
#4
|
||||
|
||||
|
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 |
|
#5
|
||||
|
||||
|
Quote:
Col
__________________
"America is pregnant with promises and anticipation, but is murdered by the hand of the inevitable." |
|
#6
|
||||
|
||||
|
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.
|
|
#7
|
||||
|
||||
|
Quote:
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." |
| Sponsored Links |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
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 |