SELECT p.name,COUNT(DISTINCT t.contact_id) unique_customers,COUNT(t.id) total_purchases,COUNT(t.id)/COUNT(DISTINCT t.contact_id) purchase_frequency 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 HAVING unique_customers>1 ORDER BY purchase_frequency DESC;