SELECT p.name,vld.qty_available current_stock,AVG(DATEDIFF(t.transaction_date,t_po.transaction_date)) avg_lead_time 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 variation_location_details vld ON p.id=vld.product_id LEFT JOIN purchase_lines pl ON tsl.product_id=pl.product_id LEFT JOIN transactions t_po ON pl.transaction_id=t_po.id WHERE t.type="sell" AND p.enable_stock=1 GROUP BY p.id,p.name,vld.qty_available ORDER BY avg_lead_time DESC;