Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

GROUP BY and alternate rows

Posted on 2005-03-03
5
Medium Priority
?
1,079 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
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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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 Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

578 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