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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Last 6 from List

Status
Not open for further replies.

MacroAlan

Programmer
Joined
Dec 4, 2006
Messages
137
Location
US
We have a table with thousands of comments on a program. Each tracking item has a number, one or more dates, and one or more comments.

I need to get the last 6 dates/comments from each tracking item.

{I don't seem to have access to upload a sample file}
Code:
ID    TrackID     Date        Comment
{auto}  202      10/20/07     Brown cow
2       202      11/02/07     Green cow




Alan

[smurf]
 
Typed, untested (SQL code):
Code:
SELECT A.TrackingID, A.CommentID, A.CommentDate, A.By, A.UpdateReason, A.LongComment
FROM JobComments AS A
INNER JOIN JobComments AS B ON A.TrackingID = B.TrackingID AND A.CommentDate <= B.CommentDate
GROUP BY A.TrackingID, A.CommentID, A.CommentDate, A.By, A.UpdateReason, A.LongComment
HAVING Count(*) <= 6

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top