SELECT patient_name, MAX(doctor) AS doctor, MAX(remark) AS remark, MAX(client_name) AS client_name, COUNT(*) AS image_count, MAX(created_at) AS last_date, (SELECT filename FROM images i2 WHERE i2.patient_name = images.patient_name AND i2.enabled = 1 ORDER BY i2.created_at DESC LIMIT 1) AS thumb_filename FROM images WHERE enabled = 1 GROUP BY patient_name ORDER BY MAX(created_at) DESC LIMIT 200
