SQL to get highest value

mrchonginhk

Well-known Member
Joined
Dec 3, 2004
Messages
679
I have a table1 like this

Country/Product/US$
===================
UK/A/200
UK/B/300
UK/C/100
US/A/200
US/B/100
US/C/500

I want a SQL to select the max sales for each country with associated product. Desired output is:-

Country/Product/US$
===================
UK/B/300
US/C/500

What should be the SQL for this ? Thanks
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi,

find below SQL...for the same..

select country, max(US) as US into #Temp_1 from table1
group by country
select Table1.country,Table1.Product, Table1.US from Table1
inner join #Temp_1 on Table1.Country =
#Temp_1.Country
and Table1.US = #Temp_1.US



OR



select Table1.country,Table1.Product, Table1.US from Table1
inner join (select country, max(US) as US from table1 group by country) as Tmp_Tbl
on Table1.Country = Tmp_Tbl.Country
and Table1.US = Tmp_Tbl.US





Regards,
Krishna
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

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