SELECT p.name,AVG(CASE WHEN tsl.quantity>1 THEN (tsl.unit_price_inc_tax-tsl.unit_price_before_discount)/tsl.unit_price_before_discount END)*100 bundle_margin,AVG(CASE WHEN tsl.quantity=1 THEN (tsl.unit_price_inc_tax-tsl.unit_price_before_discount)/tsl.unit_price_before_discount END)*100 single_margin 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 bundle_margin IS NOT NULL AND single_margin IS NOT NULL ORDER BY bundle_margin DESC;