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