SELECT * FROM ( SELECT DISTINCT ot.ticket_id, ot.number, ot.created, ot.closed, otu.name AS author, otsf.username AS assigned, otc.subject, ote.body AS description, oter.body AS resolution FROM ost_ticket ot JOIN ost_ticket__cdata otc ON ot.ticket_id = otc.ticket_id JOIN ost_ticket_status ots ON ot.status_id = ots.id JOIN ost_user otu ON ot.user_id = otu.id LEFT JOIN ost_staff otsf ON ot.staff_id = otsf.staff_id JOIN ost_thread oth ON ot.ticket_id = oth.object_id AND oth.object_type = 'T' LEFT JOIN ost_thread_entry ote ON oth.id = ote.thread_id AND ote.type = 'M' LEFT JOIN ost_thread_entry oter ON oth.id = oter.thread_id AND oter.type = 'R' WHERE ot.ticket_pid = NULL AND ots.state = 'closed' AND otc.closed BETWEEN ? AND ? ORDER BY oter.created DESC ) ot GROUP BY ot.ticket_id ORDER BY ot.assigned