SELECT p.name,vld.qty_available current_stock,AVG(CASE WHEN tsl.line_discount_amount>0 THEN tsl.quantity ELSE NULL END) promo_sales_avg,AVG(CASE WHEN tsl.line_discount_amount=0 THEN tsl.quantity ELSE NULL END) regular_sales_avg FROM products p LEFT JOIN variation_location_details vld ON p.id=vld.product_id LEFT JOIN transaction_sell_lines tsl ON p.id=tsl.product_id LEFT JOIN transactions t ON tsl.transaction_id=t.id WHERE p.enable_stock=1 AND t.type="sell" GROUP BY p.id,p.name,vld.qty_available ORDER BY promo_sales_avg DESC;