SELECT * FROM
`alert_order_history_temporary` aoh
WHERE
aoh.id IN (
SELECT
max_id
FROM
(
select
concat(aoh2.alert_id, aoh2.action_type) as group_by,
max(aoh2.id) as max_id
from
`alert_order_history_temporary` aoh2
WHERE
aoh2.order_id = 113904894
AND aoh2.alert_id = 85
AND aoh2.action_type IN ('ATTACHED', 'DETACHED')
group by
group_by
order by
max_id ASC
) AS sub
)