Solved

MySQL - Select Max Unique Order By

Posted on 2007-12-06
6
4,038 Views
Last Modified: 2013-12-12
I am trying to create a list of data where id is sorted highest to lowest, but where each name is unique.

For example:

name : id : text
aaaa : 11 :  stuff
bbbb : 12 :  more stuff
aaaa : 13 :  other stuff
bbbb : 14 :  more other stuff

Query should return only one name for it's highest id only so:

bbbb : 14 :  more other stuff
aaaa : 13 :  other stuff

Thanks for reading.
SELECT name, MAX(id) AS id, text, 

			option 

			FROM dataset1 

			LEFT OUTER JOIN dataset2 ON name_fk = name  

			WHERE something!=1

			GROUP BY name

			ORDER BY id DESC

			LIMIT 15;

Open in new window

0
Comment
Question by:ncoo
  • 3
  • 2
6 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20422759
SELECT name, id, text,
                        option
                        FROM dataset1
                        LEFT OUTER JOIN dataset2 ON name_fk = name  
                        WHERE something!=1
                           AND id = (SELECT MAX(ID) from dataset1 x where x.name = dataset1.name)
                        GROUP BY name
                        ORDER BY id DESC
                        LIMIT 15;
0
 
LVL 15

Author Comment

by:ncoo
ID: 20424190
Thanks, what about:

For example:

name : id : text : other :
aaaa : 11 :  stuff : 5 :
bbbb : 12 :  more stuff : 10 :
aaaa : 13 :  other stuff : 12 :
bbbb : 14 :  more other stuff : 1 :

Query should return only one name for it's highest other only so:

aaaa : 13 :  other stuff : 12 :
bbbb : 12 :  more stuff : 10 :

the field other is part of table dataset1
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20424525
SELECT * FROM
(
SELECT name, id, text, other
FROM dataset1
LEFT OUTER JOIN dataset2 ON name_fk = name  
WHERE something!=1
GROUP BY name
ORDER BY other desc
) AS Y
ORDER BY id DESC
LIMIT 15;
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 5

Assisted Solution

by:ursangel
ursangel earned 100 total points
ID: 20426617
i hope you always need only the highest id , what ever your field names are.
Most simple way seems like this.
Check this out...
select id, ename, text from Data a
where id in (select max(id) from Data group by ename)
order by id
0
 
LVL 15

Author Comment

by:ncoo
ID: 20427334
The problem with that imitchie is that it sorts the whole table, is there anyway to avoid sorting the whole table before selecting just the 15 records?
0
 
LVL 25

Accepted Solution

by:
imitchie earned 400 total points
ID: 20431117
The problem is that the way you have prescribed the problem domain requires that you do sort everything, group them in a Particular order, then Sort them by a secondary order, however the limit 15 is applied to the Secondary order!  You might just want to revisit what you can live with.  Because there is an inherent dependancy in there.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 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

21 Experts available now in Live!

Get 1:1 Help Now