Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query last comment for each account

Status
Not open for further replies.

xyle

Programmer
Jul 2, 2003
23
US
I need to make a query to get the last comment for each account. Each record has a timestamp I can use to find the last comment. The table is called tblActions, primary key is Action Id, the fields I need are AccountNo, DateOfAction, and Comments. The query I tried is

Code:
SELECT tblActions.AccountNo, Max(tblActions.DateOfAction) AS MaxOfDateOfAction, tblActions.Comments
FROM tblActions
GROUP BY tblActions.AccountNo, tblActions.Comments;

It gives me every record because of the Group By tblActions.Comments but I don’t know how to get around that and the primary key is comprised of a few different things so it is not exactly sequential.
 
What about this ?
Code:
SELECT A.AccountNo, A.DateOfAction, A.Comments
FROM tblActions A INNER JOIN (
SELECT AccountNo, Max(DateOfAction) AS MaxOfDateOfAction FROM tblActions GROUP BY AccountNo
) L ON A.AccountNo=L.AccountNo AND A.DateOfAction=L.MaxOfDateOfAction

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks I would never have gotten that
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top