I created a cursor from the one that was described on this post: SQL – Call Stored Procedure for each record
But what I really want is a set based solution. I have two tables, Tickets and WorkLogs.
Each Ticket can have multiple worklogs, I just want the most recent for each ticket.
SELECT WorkLog.WorkLogDate, WorkLog.TextEntry, Ticket.ID,
Ticket.Summary, Ticket.Requester, Ticket.Status, Ticket.Priority,
Ticket.AssignedTo, Ticket.DateResolved, Ticket.TimeSpent
FROM Ticket INNER JOIN WorkLog ON Ticket.ID = WorkLog.TicketIDRef
If I could somehow SELECT TOP (1) _WorkLog_ FROM WorkLog ORDER BY WorkLogID DESC for each Ticket.ID in Tickets I would have the set I’m looking for. I saw some similar solutions using CROSS APPLY but I’m not sure what function I would need to apply.
Any help getting my brain out of OO gear is greatly appreciated.
There are several ways of doing it.
1) NOT EXISTS
2) Sub select
3) Joins