Solved

Use of MAX, GROUP BY

Posted on 2007-03-27
5
4,622 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 100 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 400 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

735 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