SELECT c.name,DATE_FORMAT(t.transaction_date,"%Y-%m") month,COUNT(t.id) transactions,SUM(t.final_total) spend FROM contacts c JOIN transactions t ON c.id=t.contact_id WHERE t.type="sell" GROUP BY c.id,c.name,DATE_FORMAT(t.transaction_date,"%Y-%m") ORDER BY c.name,month;