select COUNT(*), q.flag_count, AVG(TIMESTAMPDIFF(SECOND, p.`created_at`, p.`deleted_at`)) as delta from (select COUNT(*) as flag_count, f.post_id
from flag_logs f
where f.is_auto = 1
group by f.post_id
order by f.post_id desc) q
inner join posts p on p.id = q.post_id
inner join sites s on s.id = p.site_id
where p.created_at > '2018-02-26 04:03:00'
and p.`deleted_at` IS NOT NULL
and s.`max_flags_per_post` = 3
group by flag_count