SELECT
    CASE 
        WHEN c.sActual_Dest_ID LIKE 'S02%' THEN 'S02'
        ELSE 'S01'
    END AS Sorter,
    DATE_FORMAT(:starttime, '%Y-%m-%d %H:%i') AS start_time,
    DATE_FORMAT(:endtime, '%Y-%m-%d %H:%i') AS end_time,
    COALESCE(c.sActual_Dest_ID, 'N/A') AS Lane,
    CONCAT(COALESCE(c.Success_pct, 0), '%') AS success_perc,
    CONCAT(COALESCE(c.Unknown_pct, 0), '%') AS unknown_perc,
    CONCAT(COALESCE(c.Unexpected_Container_pct, 0), '%') AS unexpected_container_perc,
    CONCAT(COALESCE(c.Tracking_Error_pct, 0), '%') AS tracking_error_perc,
    CONCAT(COALESCE(c.Gap_Error_pct, 0), '%') AS gap_error_perc,
    CONCAT(COALESCE(c.Destination_Full_pct, 0), '%') AS destination_full_perc,
    CONCAT(COALESCE(c.Destination_Non_Operational_pct, 0), '%') AS destination_non_operational_perc,
    CONCAT(COALESCE(c.Invalid_Destination_pct, 0), '%') AS invalid_destination_perc,
    CONCAT(COALESCE(c.Scanner_Error_pct, 0), '%') AS scan_error_perc,
    CONCAT(COALESCE(c.Destination_Disabled_pct, 0), '%') AS destination_disabled_perc,
    CONCAT(COALESCE(c.Throughput_Limit_pct, 0), '%') AS throughput_limit_perc,
    CONCAT(COALESCE(c.Failed_To_Divert_pct, 0), '%') AS failed_to_divert_perc,
    CONCAT(COALESCE(c.No_Destination_Received_pct, 0), '%') AS no_destination_received_perc,
    CONCAT(COALESCE(c.Lost_Container_pct, 0), '%') AS lost_container_perc,
    CONCAT(COALESCE(c.Dimension_Error_pct, 0), '%') AS dimension_error_perc,
    CONCAT(COALESCE(c.Weight_Error_pct, 0), '%') AS weight_error_perc,
    CONCAT(COALESCE(c.Container_Utilization_pct, 0), '%') AS container_utilization_perc,
    CONCAT(COALESCE(c.Unable_To_Divert_pct, 0), '%') AS unable_to_divert_perc,
    CONCAT(COALESCE(c.Destination_Not_Attempted_pct, 0), '%') AS destination_not_attempted_perc,
    COALESCE(c.total, 0) AS total
FROM (SELECT 1) AS p
LEFT JOIN (
    SELECT
        sActual_Dest_ID,
        SUM(CASE WHEN adiSort_Code_0 = 0 THEN 1 ELSE 0 END) AS Success_count,
        SUM(CASE WHEN adiSort_Code_0 = 1 THEN 1 ELSE 0 END) AS Unknown_count,
        SUM(CASE WHEN adiSort_Code_0 = 2 THEN 1 ELSE 0 END) AS Unexpected_Container_count,
        SUM(CASE WHEN adiSort_Code_0 = 3 THEN 1 ELSE 0 END) AS Tracking_Error_count,
        SUM(CASE WHEN adiSort_Code_0 = 4 THEN 1 ELSE 0 END) AS Gap_Error_count,
        SUM(CASE WHEN adiSort_Code_0 = 5 THEN 1 ELSE 0 END) AS Destination_Full_count,
        SUM(CASE WHEN adiSort_Code_0 = 6 THEN 1 ELSE 0 END) AS Destination_Non_Operational_count,
        SUM(CASE WHEN adiSort_Code_0 = 7 THEN 1 ELSE 0 END) AS Invalid_Destination_count,
        SUM(CASE WHEN adiSort_Code_0 IN (8, 9, 10) THEN 1 ELSE 0 END) AS Scanner_Error_count,
        SUM(CASE WHEN adiSort_Code_0 = 12 THEN 1 ELSE 0 END) AS Destination_Disabled_count,
        SUM(CASE WHEN adiSort_Code_0 = 13 THEN 1 ELSE 0 END) AS Throughput_Limit_count,
        SUM(CASE WHEN adiSort_Code_0 = 14 THEN 1 ELSE 0 END) AS Failed_To_Divert_count,
        SUM(CASE WHEN adiSort_Code_0 = 16 THEN 1 ELSE 0 END) AS No_Destination_Received_count,
        SUM(CASE WHEN adiSort_Code_0 = 17 THEN 1 ELSE 0 END) AS Lost_Container_count,
        SUM(CASE WHEN adiSort_Code_0 = 18 THEN 1 ELSE 0 END) AS Dimension_Error_count,
        SUM(CASE WHEN adiSort_Code_0 = 19 THEN 1 ELSE 0 END) AS Weight_Error_count,
        SUM(CASE WHEN adiSort_Code_0 = 20 THEN 1 ELSE 0 END) AS Container_Utilization_count,
        SUM(CASE WHEN adiSort_Code_0 = 21 THEN 1 ELSE 0 END) AS Unable_To_Divert_count,
        SUM(CASE WHEN adiSort_Code_0 = 22 THEN 1 ELSE 0 END) AS Destination_Not_Attempted_count,
        COUNT(*) AS total,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 0 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Success_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 1 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Unknown_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 2 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Unexpected_Container_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 3 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Tracking_Error_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 4 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Gap_Error_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 5 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Destination_Full_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 6 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Destination_Non_Operational_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 7 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Invalid_Destination_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 IN (8, 9, 10) THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Scanner_Error_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 12 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Destination_Disabled_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 13 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Throughput_Limit_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 14 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Failed_To_Divert_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 16 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS No_Destination_Received_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 17 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Lost_Container_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 18 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Dimension_Error_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 19 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Weight_Error_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 20 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Container_Utilization_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 21 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Unable_To_Divert_pct,
        CASE WHEN COUNT(*) = 0 THEN 0 ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 22 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2) END AS Destination_Not_Attempted_pct
    FROM item_data
    WHERE t_stamp BETWEEN :starttime AND :endtime
        AND adiSort_Code_0 NOT IN (11, 15)
    GROUP BY sActual_Dest_ID
) AS c ON 1=1;

