Finding Max values in groups with a query

JHSam

Board Regular
Joined
Feb 17, 2002
Messages
62
I'm having some problems with a query to do the following: Pick the highest value for Field "X" and its' corresponding value in Field "Y", from a group of values in Field "X" with a given characteristic. An example follows:

Field1 FieldX FieldY
Blue 23 9
Blue 16 10
Red 12 3
Red 6 11
Red 3 8

So for Blue, it returns a value of 23 (for Field X) and 9 (for Field Y). For Red, it returns a value of 12 (for Field X) and 3 for Field Y.

I thought that I could run this using the group function, but it doesn't seem to be working.

Any suggestions would be appreciated.

JHSam
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi JHSam,

Try SQL as follows:

SELECT TOP 1 Tbl1.Field1, Tbl1.FieldX, Tbl1.FieldY
FROM Tbl1
WHERE (((Tbl1.Field1)=[Enter Color]))
ORDER BY Tbl1.FieldX DESC;

To use this SQL, select "SQL" from that icon that lets you choose your query view (design/datasheet/SQL).

Paste in the above, replacing whatever might be there.

You will probably have to adjust Tbl1 to your table and likewise with the field names.

HTH,
 
Upvote 0
Thanks -- that worked if I wanted to see the output for each value in Field1 (i.e., colors, in my example) individually. I was looking more for the query output the results for all of the colors in one pass -- my apologies for being unclear on this in my original message.

I thought the domain aggregate functions might work, but these rolled together all of the records as opposed to the records for each value in Field1.
 
Upvote 0
Try this. My table is named tbl060303, so change all instances of this to your table name. My fields are as you used in your example.

SELECT A.Field1, A.MaxFieldX, B.FieldY FROM
(SELECT Field1, Max(FieldX) AS MaxFieldX FROM tbl060303 GROUP BY Field1) AS A
INNER JOIN (SELECT * FROM tbl060303) AS B ON A.Field1=B.Field1 and A.MaxFieldX=B.FieldX;


HTH,

Russell
 
Upvote 0
Thanks for the help. Before I viewed your posting, I had figured out a way to do it with a combination of a crosstab query and select query. This gave comparable results to that from your posting.

:)
 
Upvote 0
Yep, multiple queries is always a good route (but you shouldn't need a crosstab).

And I hope the results were not only comparable, but exactly the same! :biggrin:

-rh
 
Upvote 0

Forum statistics

Threads
1,221,531
Messages
6,160,352
Members
451,639
Latest member
Kramb

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top