Get the last row


Let's say I have the following rows

MyAutoinc     GroupId    Amount
1                     A             2000
2                     A            3000
3                     A            2000
4                     A            5000
5                    A            2000
6                    B            2000
7                    B            8000
8                    C            4000
9                   C            9000
10                    C            4000
11                    C            2000

I want to get the last row per every group so the result should be:

5                    A            2000
7                    B            8000
11                    C            2000

Thank you
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.

this should do what you want

select *  from table  where MyAutoinc  in( select max(MyAutoinc ) from table group by GroupId )


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
Kevin CrossChief Technology OfficerCommented:
starhu, I was passing through the zone and noticed this was still open. The above comment should solve this for you. However, for performance reasons, you may want to go with JOIN.

SELECT t.MyAutoinc, t.GroupId, t.Amount
FROM your_table t
   SELECT GroupId, MAX(MyAutoinc) AS MAX_MyAutoinc
   FROM your_table
   GROUP BY GroupId
) m ON m.GroupId = t.GroupId AND m.MAX_MyAutoinc = t.MyAutoinc

Anyway, since this is still open, I just wanted to support the above http:#36958894 should work.

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.