SELECT 
    p.name,
    p.sku,
    SUM(tsl.quantity) as total_sold,
    COALESCE(vld.qty_available, 0) as current_stock,
    CASE 
        WHEN COALESCE(vld.qty_available, 0) > 0 
        THEN SUM(tsl.quantity)/COALESCE(vld.qty_available, 1) 
        ELSE 0 
    END as turnover_rate
FROM products p
LEFT JOIN transaction_sell_lines tsl ON p.id = tsl.product_id
LEFT JOIN variation_location_details vld ON p.id = vld.product_id
WHERE p.enable_stock = 1
GROUP BY p.id, p.name, p.sku, vld.qty_available
ORDER BY turnover_rate DESC;