?
Solved

SQL to find max value in each group

Posted on 2003-02-24
7
Medium Priority
?
365 Views
Last Modified: 2012-08-13
I have data like this .

Name    Value
A       12
B       30
A       15
B       45

I would like to query max value in each
name. The answer is A 15 and B 45.
Could you give me a SQL for this question

Thank you,
0
Comment
Question by:eci
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Expert Comment

by:mrstick
ID: 8007423
This should do it:

SELECT TempTable.Name, Max(TempTable.Value) AS MaxOfValue FROM TempTable GROUP BY TempTable.Name;

Assuming that your table is called TempTable.

HTH

Carl
0
 

Author Comment

by:eci
ID: 8007457
Thank you,

I have another tabel like this.

Name    Value  Adress
A       12     Cat
B       30     Rat
A       15     Bat
B       45     Dog


Can i get this answer?

A       15     Bat
B       45     Dog
0
 
LVL 1

Expert Comment

by:mrstick
ID: 8007684
Hi,

I'm not actually that sure how to do it, the following works, but I suspect that there is an easier way:

SELECT TempTable.Name, TempTable.Value, TempTable.Address FROM Query3 INNER JOIN TempTable ON (Query3.MaxOfValue = TempTable.Value) AND (Query3.Name = TempTable.Name);

Query3 is basically the query from my first response.

HTH

Carl
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

Expert Comment

by:tiendh
ID: 8008262
SQL in Oracle
select name, max(value), address
from table
group by name, address
0
 
LVL 1

Expert Comment

by:mrstick
ID: 8008476
I tried that but couldn't get it to work in Access, but then again what can you get to work in Access..?!? ;o)

C
0
 

Expert Comment

by:tomArghh
ID: 8009320
tiendh is quite right, for Access though, the last line needs to read;

order by name, address
0
 

Accepted Solution

by:
MBro earned 150 total points
ID: 8014942
here is in Access :
-------------------

SELECT [myTable].[name], table2.maxValue, [myTable].[address]
FROM myTable, [SELECT name, MAX(value) AS maxValue FROM myTable GROUP BY name]. AS table2
WHERE ([myTable].[name]=table2.name) AND
([myTable].[value]=table2.maxValue);
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question