Minimum function with multiple categories

FredMcStaire

New Member
Joined
Jan 19, 2009
Messages
36
I would like to find, for each car make, what colour has the maximum sales. So it would return Ford, Blue, 2; Holden, Blue,5; Toyota, red,11.
( this is a dummy set of data, in the real set make and colour each have upwards of 50 unique values)
a data set as follows:
MakeColourSales
Fordred1
FordBlue2
HoldenRed3
HoldenBlue5
Toyotablue7
Toyotared11

<COLGROUP><COL style="WIDTH: 48pt" span=3 width=64><TBODY>
</TBODY>

Any advice on whether this is possible with a single query without VBA?

Thanks
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
If you want to do it in a Single Query, you will need to write the SQL code directly instead of using the Query Builder, as it involves a Nested Query (you could use the Query Builder if you are doing it in two steps/queries).

Here is what that SQL Code might look like:
Code:
SELECT CarsTable.Make, CarsTable.Colour,CarsTable.Sales
FROM CarsTable
INNER JOIN
(SELECT CarsTable.Make, Max(CarsTable.Sales) AS MaxOfSales
FROM CarsTable
GROUP BY CarsTable.Make) as X
ON CarsTable.Make=X.Make
AND CarsTable.Sales=X.MaxOfSales;

Basically, you are just first performing an Aggregate Query to find the maximum count for each Make, then linking that back to your original table to get the Colour associated with that Make/Sales combination.
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,385
Members
448,956
Latest member
JPav

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