SELECT c.name supplier,COUNT(t.id) purchase_orders,AVG(DATEDIFF(tp.paid_on,t.transaction_date)) avg_payment_terms 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="purchase" GROUP BY c.id,c.name ORDER BY avg_payment_terms DESC;