SELECT cg.name customer_group,COUNT(DISTINCT c.id) customers,SUM(t.final_total) total_revenue,AVG(t.final_total) avg_transaction_value,SUM(t.final_total)/COUNT(DISTINCT c.id) lifetime_value FROM customer_groups cg JOIN contacts c ON cg.id=c.customer_group_id JOIN transactions t ON c.id=t.contact_id WHERE t.type="sell" GROUP BY cg.id,cg.name ORDER BY lifetime_value DESC;