SELECT p.name,SUM(tsl.quantity) sales_volume,vld.qty_available current_stock,SUM(tsl.quantity)/NULLIF(vld.qty_available,0) turnover_ratio 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 WHERE t.type="sell" AND t.transaction_date>=DATE_SUB(CURRENT_DATE,INTERVAL 30 DAY) GROUP BY p.id,p.name,vld.qty_available HAVING current_stock>0 ORDER BY turnover_ratio DESC;