WITH latest AS (
    -- Get latest row per S02 lane
    SELECT ld.*
    FROM lane_data ld
    INNER JOIN (
        SELECT Name, MAX(t_stamp) AS max_ts
        FROM lane_data
        WHERE Name LIKE 'S02\\_%'
        GROUP BY Name
    ) x ON ld.Name = x.Name AND ld.t_stamp = x.max_ts
),
totals AS (
    SELECT
        SUM(CASE WHEN Disabled = 1 THEN 1 ELSE 0 END) AS disabled_count,
        SUM(CASE WHEN Disabled = 0 THEN 1 ELSE 0 END) AS enabled_count,
        COUNT(*) AS total_lanes
    FROM latest
)
SELECT
    'Enabled' AS Status,
    enabled_count AS Count,
    CONCAT(ROUND(enabled_count * 100.0 / total_lanes, 1), '%') AS `Percentage (%)`
FROM totals

UNION ALL

SELECT
    'Disabled' AS Status,
    disabled_count AS Count,
    CONCAT(ROUND(disabled_count * 100.0 / total_lanes, 1), '%') AS `Percentage (%)`
FROM totals;
