SELECT c.name,COUNT(t.id) purchases,COUNT(DISTINCT t2.contact_id) influenced_customers,SUM(t.final_total) total_spent FROM contacts c JOIN transactions t ON c.id=t.contact_id LEFT JOIN transactions t2 ON t.transaction_date<t2.transaction_date WHERE t.type="sell" GROUP BY c.id,c.name ORDER BY influenced_customers DESC;