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,
    COALESCE(c.Success, 0) AS success_count,
    COALESCE(c.Unknown, 0) AS unknown_count,
    COALESCE(c.Unexpected_Container, 0) AS unexpected_container_count,
    COALESCE(c.Tracking_Error, 0) AS tracking_error_count,
    COALESCE(c.Gap_Error, 0) AS gap_error_count,
    COALESCE(c.Destination_Full, 0) AS destination_full_count,
    COALESCE(c.Destination_Non_Operational, 0) AS destination_non_operational_count,
    COALESCE(c.Invalid_Destination, 0) AS invalid_destination_count,
    COALESCE(c.Scanner_Error, 0) AS scan_error_count,
    COALESCE(c.Destination_Disabled, 0) AS destination_disabled_count,
    COALESCE(c.Throughput_Limit, 0) AS throughput_limit_count,
    COALESCE(c.Failed_To_Divert, 0) AS failed_to_divert_count,
    COALESCE(c.No_Destination_Received, 0) AS no_destination_received_count,
    COALESCE(c.Lost_Container, 0) AS lost_container_count,
    COALESCE(c.Dimension_Error, 0) AS dimension_error_count,
    COALESCE(c.Weight_Error, 0) AS weight_error_count,
    COALESCE(c.Container_Utilization, 0) AS container_utilization_count,
    COALESCE(c.Unable_To_Divert, 0) AS unable_to_divert_count,
    COALESCE(c.Destination_Not_Attempted, 0) AS destination_not_attempted_count,
    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,
        SUM(CASE WHEN adiSort_Code_0 = 1 THEN 1 ELSE 0 END) AS Unknown,
        SUM(CASE WHEN adiSort_Code_0 = 2 THEN 1 ELSE 0 END) AS Unexpected_Container,
        SUM(CASE WHEN adiSort_Code_0 = 3 THEN 1 ELSE 0 END) AS Tracking_Error,
        SUM(CASE WHEN adiSort_Code_0 = 4 THEN 1 ELSE 0 END) AS Gap_Error,
        SUM(CASE WHEN adiSort_Code_0 = 5 THEN 1 ELSE 0 END) AS Destination_Full,
        SUM(CASE WHEN adiSort_Code_0 = 6 THEN 1 ELSE 0 END) AS Destination_Non_Operational,
        SUM(CASE WHEN adiSort_Code_0 = 7 THEN 1 ELSE 0 END) AS Invalid_Destination,
        SUM(CASE WHEN adiSort_Code_0 IN (8, 9, 10) THEN 1 ELSE 0 END) AS Scanner_Error,
        SUM(CASE WHEN adiSort_Code_0 = 12 THEN 1 ELSE 0 END) AS Destination_Disabled,
        SUM(CASE WHEN adiSort_Code_0 = 13 THEN 1 ELSE 0 END) AS Throughput_Limit,
        SUM(CASE WHEN adiSort_Code_0 = 14 THEN 1 ELSE 0 END) AS Failed_To_Divert,
        SUM(CASE WHEN adiSort_Code_0 = 16 THEN 1 ELSE 0 END) AS No_Destination_Received,
        SUM(CASE WHEN adiSort_Code_0 = 17 THEN 1 ELSE 0 END) AS Lost_Container,
        SUM(CASE WHEN adiSort_Code_0 = 18 THEN 1 ELSE 0 END) AS Dimension_Error,
        SUM(CASE WHEN adiSort_Code_0 = 19 THEN 1 ELSE 0 END) AS Weight_Error,
        SUM(CASE WHEN adiSort_Code_0 = 20 THEN 1 ELSE 0 END) AS Container_Utilization,
        SUM(CASE WHEN adiSort_Code_0 = 21 THEN 1 ELSE 0 END) AS Unable_To_Divert,
        SUM(CASE WHEN adiSort_Code_0 = 22 THEN 1 ELSE 0 END) AS Destination_Not_Attempted,
        COUNT(*) AS total
    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;

