SELECT p.name,vld.qty_available current_stock,STDDEV(daily_sales.quantity) demand_variability FROM products p LEFT JOIN variation_location_details vld ON p.id=vld.product_id LEFT JOIN (SELECT product_id,DATE(transaction_date) sale_date,SUM(quantity) quantity FROM transaction_sell_lines tsl JOIN transactions t ON tsl.transaction_id=t.id WHERE t.type="sell" GROUP BY product_id,DATE(transaction_date)) daily_sales ON p.id=daily_sales.product_id WHERE p.enable_stock=1 GROUP BY p.id,p.name,vld.qty_available ORDER BY demand_variability DESC;