Hi,
I am going crazy with a problem and would really appreciate if anybody could help me with it!! Here is what I am trying to achieve:
I have two tables - "tickets" and "ticket_updates"
tickets has following columns:
TicketId, UserId, AssignedToId, ... DateCreated, Status
ticket_updates has following columns:
UpdateId, TicketId, UpdaterId, ... DateUpdated
I am trying to select the rows from the ticket_updates table that has the latest update [MAX(ticket_updates.DateUpdated)] for all the open tickets (tickets.status = 1)
Here is what I have:
SELECT ticket_updates.*,
MAX( ticket_updates.DateUpdated ) AS DateUpdated
FROM ticket_updates
LEFT JOIN tickets ON tickets.TicketId = ticket_updates.TicketId
WHERE tickets.status = 1
GROUP BY ticket_updates.TicketId
But obviously (as I figured out now) this doesn't give me the right rows.
I have tried do a sub select as in:
WHERE ...
ticket_updates.DateUpdated = (SELECT MAX( ticket_updates.DateUpdated )
FROM ticket_updates
GROUP BY ticketid)
but obviously (again) this doesn't help since the sub select fetches multiple rows. And if I exclude the GROUP BY then I would get only one row in the output which would have the max DateUpdated. I am looking to fetch the rows containing the last updates for all the tickets that are open (status = 1).
I have tried some others as well (including HAVING) but I am just unable to get there
Any help is really appreciated.
Thanks!
Syed