SELECT c.name customer,AVG(DATEDIFF(tp.paid_on,t.transaction_date)) avg_collection_days,COUNT(t.id) transaction_count,SUM(t.final_total) total_sales FROM contacts c JOIN transactions t ON c.id=t.contact_id JOIN transaction_payments tp ON t.id=tp.transaction_id WHERE t.type="sell" GROUP BY c.id,c.name ORDER BY avg_collection_days DESC;