SELECT cg.name customer_group,COUNT(t.id) transactions,SUM(t.final_total) total_sales 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 total_sales DESC;