Solved

Use of MAX, GROUP BY

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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

815 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now