SELECT
d.name AS `Department`,
(a.tot/COUNT(t.id)) AS `Average replies`
FROM hb_ticket_departments d JOIN
(
 SELECT COUNT(r.id) as tot, t.dept_id FROM hb_ticket_replies r JOIN hb_tickets t ON (t.id=r.ticket_id)
 GROUP BY t.dept_id
) a ON (a.dept_id=d.id)
JOIN hb_tickets t ON (t.dept_id=d.id)
GROUP BY t.dept_id
