SELECT c.name,AVG(TIMESTAMPDIFF(MINUTE,t.created_at,t.updated_at)) avg_processing_time,COUNT(t.id) total_visits FROM contacts c JOIN transactions t ON c.id=t.contact_id WHERE t.type="sell" GROUP BY c.id,c.name ORDER BY avg_processing_time;