SELECT * FROM (
SELECT MAX(c5_.id) AS sclr_32 FROM contacts_history c5_
WHERE (c5_.organization_id = '1') GROUP BY c5_.contact_id
) tt
JOIN contacts_history ch ON tt.sclr_32 = ch.id;
SELECT * FROM contacts_history WHERE id IN (
SELECT MAX(c5_.id) AS sclr_32 FROM contacts_history c5_ WHERE (c5_.organization_id = '1') GROUP BY c5_.contact_id
);