Solved

MySQL - Select Max Unique Order By

Posted on 2007-12-06
6
4,048 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

820 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