Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MySQL - Select Max Unique Order By

Posted on 2007-12-06
6
Medium Priority
?
4,052 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
[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
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 5

Assisted Solution

by:ursangel
ursangel earned 400 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 1600 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 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