GROUP BY and alternate rows

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
phasevarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ldbkuttyCommented:
SELECT MAX(num), letter
FROM sample
GROUP BY letter
ORDER BY num DESC
0
phasevarAuthor Commented:
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
as2003Commented:
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
as2003Commented:
On second thoughts, this should work:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trommCommented:
yes. The max-function is not influenced by order by.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.