SELECT HOUR(t.transaction_date) hour,COUNT(DISTINCT t.contact_id) unique_customers,COUNT(DISTINCT cg.id) customer_groups,AVG(t.final_total) avg_transaction FROM transactions t JOIN contacts c ON t.contact_id=c.id LEFT JOIN customer_groups cg ON c.customer_group_id=cg.id WHERE t.type="sell" GROUP BY HOUR(t.transaction_date) ORDER BY unique_customers DESC;