SELECT c.name,MAX(t.transaction_date) last_purchase,DATEDIFF(CURRENT_DATE,MAX(t.transaction_date)) days_since_purchase,COUNT(t.id) total_purchases FROM contacts c LEFT JOIN transactions t ON c.id=t.contact_id AND t.type="sell" GROUP BY c.id,c.name HAVING days_since_purchase>90 ORDER BY days_since_purchase DESC;