SELECT p.name,COUNT(DISTINCT t.contact_id) customers,SUM(tsl.quantity) units,SUM(tsl.quantity)/COUNT(DISTINCT t.contact_id) units_per_customer FROM products p JOIN transaction_sell_lines tsl ON p.id=tsl.product_id JOIN transactions t ON tsl.transaction_id=t.id WHERE t.type="sell" GROUP BY p.id,p.name ORDER BY units_per_customer DESC;