SELECT c.id customer,COUNT(t.id) total_purchases,SUM(t.final_total) total_spent,AVG(t.final_total) avg_purchase FROM contacts c JOIN transactions t ON c.id=t.contact_id WHERE t.type="sell" GROUP BY c.id ORDER BY total_purchases DESC;