Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get the last row

Posted on 2011-10-12
2
Medium Priority
?
266 Views
Last Modified: 2012-06-27
Hello,

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
0
Comment
Question by:starhu
2 Comments
 
LVL 2

Accepted Solution

by:
jmgst116 earned 1000 total points
ID: 36958894
this should do what you want

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

0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 1000 total points
ID: 36974840
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
JOIN (
   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.

Kevin
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
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 Month21 days, 1 hour left to enroll

810 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