?
Solved

Use of MAX, GROUP BY

Posted on 2007-03-27
5
Medium Priority
?
4,632 Views
Last Modified: 2010-04-27
Hi,
I've a query that returns me 3 data:

select number, name, editor
from table

I'd like to have the max(number),name, editor for each editor.
Exemple: the table:
result     name      editor
50          toto         A
60          dada       A
20          toto         B
10          dada       B

I want the follwoing result:
result     name    editor
60          dada     A
20          toto       B

Thanks & Regards.


0
Comment
Question by:bruno_boccara
[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
5 Comments
 
LVL 11

Expert Comment

by:Ved Prakash Agrawal
ID: 18799915
select max(number) as result, name, editor
from table
group by number, editor
0
 
LVL 11

Expert Comment

by:Ved Prakash Agrawal
ID: 18799925
if you want to sort the result you can also tried to add "Order by" Clause
0
 
LVL 20

Assisted Solution

by:Sirees
Sirees earned 400 total points
ID: 18799938
Try this

select a.result, a.name, a.editor
from yourtable a
where a.result = (select max(b.result) from yourtable b where a.editor = b.editor)
0
 

Author Comment

by:bruno_boccara
ID: 18800190
In fact my table is not one table , but a result of a join between 3 tables .....
Sirees Solution works, but not in my case.
About ved17nov's solution, it doesn't work because you didn't include name into the group by.
so it returns me an error msg.
0
 
LVL 2

Accepted Solution

by:
jhshen earned 1600 total points
ID: 18800828
This is a sample query to resolve your problem.

select t1.* from mytable t1
inner join (select max(number) as maxNumber, editor from mytable group by editor) t2
on t1.number = t2.maxNumber and t1.editor = t2.editor
order by t1.editor
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

800 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