SELECT p.name,COUNT(DISTINCT t.id) sales,COUNT(DISTINCT t2.id) cross_sales FROM products p JOIN transaction_sell_lines tsl ON p.id=tsl.product_id JOIN transactions t ON tsl.transaction_id=t.id LEFT JOIN transactions t2 ON t.contact_id=t2.contact_id AND t2.id>t.id WHERE t.type="sell" GROUP BY p.id,p.name ORDER BY cross_sales DESC;