SELECT c.name supplier,COUNT(t.id) purchases,AVG(pl.purchase_price) avg_cost,STDDEV(pl.purchase_price) cost_variability FROM contacts c JOIN transactions t ON c.id=t.contact_id JOIN purchase_lines pl ON t.id=pl.transaction_id WHERE t.type="purchase" GROUP BY c.id,c.name ORDER BY cost_variability;