SELECT p.name,SUM(tsl.quantity) sales_volume,vld.qty_available current_stock,SUM((tsl.unit_price_inc_tax-tsl.unit_price_before_discount)*tsl.quantity)/NULLIF(vld.qty_available*v.default_purchase_price,0)*100 roi FROM products p JOIN variations v ON p.id=v.product_id LEFT JOIN variation_location_details vld ON p.id=vld.product_id 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,vld.qty_available,v.default_purchase_price HAVING current_stock>0 ORDER BY roi DESC;