SELECT p.name,COUNT(DISTINCT t.contact_id) visitors,COUNT(DISTINCT CASE WHEN t.type="sell" THEN t.id END) purchases,(COUNT(DISTINCT CASE WHEN t.type="sell" THEN t.id END)*100.0/COUNT(DISTINCT t.contact_id)) conversion_rate FROM products p JOIN transaction_sell_lines tsl ON p.id=tsl.product_id JOIN transactions t ON tsl.transaction_id=t.id GROUP BY p.id,p.name ORDER BY conversion_rate DESC;