?
Solved

GROUP BY and alternate rows

Posted on 2005-03-03
5
Medium Priority
?
1,065 Views
Last Modified: 2012-06-27
I'm trying to eliminate duplicate rows of a particular column in a select query.  Group by allows me to do that but it is returning the first row inserted instead, I need the last row inserted.

Here's a sample table:

+------+--------+
| num  | letter |
+------+--------+
|    1 | A      |
|    2 | A      |
|    3 | B      |
|    4 | B      |
+------+--------+

Here's a sample query:

mysql> select num, letter from sample GROUP BY letter ORDER BY num DESC;
+------+--------+
| num  | letter |
+------+--------+
|    3 | B      |
|    1 | A      |
+------+--------+

I'm looking to get num 2 & 4, not num 1 & 3.

I'm running MySQL 4.0
0
Comment
Question by:phasevar
[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 32

Expert Comment

by:ldbkutty
ID: 13454754
SELECT MAX(num), letter
FROM sample
GROUP BY letter
ORDER BY num DESC
0
 

Author Comment

by:phasevar
ID: 13454841
Hmmm.. Is there a way to accomplish that if I'm already using ORDER BY on a different column?  Such as this:

+------+--------+------+
| num  | letter | str  |
+------+--------+------+
|    1 | A      | zzz  |
|    2 | A      | yyy  |
|    3 | B      | xxx  |
|    4 | B      | www  |
+------+--------+------+

mysql> SELECT num, letter, str FROM sample GROUP BY letter ORDER BY str DESC;
+------+--------+------+
| num  | letter | str  |
+------+--------+------+
|    1 | A      | zzz  |
|    3 | B      | xxx  |
+------+--------+------+

As above, I'm looking for num to be 2 & 4.
0
 
LVL 4

Expert Comment

by:as2003
ID: 13456618
As you're using MySQL 4.0 there isn't going to be an elegant solution. Have a look at this: http://dev.mysql.com/doc/mysql/en/example-maximum-column-group-row.html (and consider upgrading to v4.1 or greater!)
0
 
LVL 4

Accepted Solution

by:
as2003 earned 2000 total points
ID: 13456643
On second thoughts, this should work:

SELECT MAX(num), lette, str
FROM sample
GROUP BY letter
ORDER BY str DESC
0
 
LVL 4

Expert Comment

by:tromm
ID: 13456653
yes. The max-function is not influenced by order by.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

771 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