SELECT
    CASE  
        WHEN COALESCE(c.sLocation_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(NULLIF(c.sLocation_ID, ''), 'N/A') AS sLocation_ID,
    COALESCE(c.Success_rate, 0) AS success_rate,
    COALESCE(c.Unknown_rate, 0) AS unknown_rate,
    COALESCE(c.Unexpected_Container_rate, 0) AS unexpected_container_rate,
    COALESCE(c.Tracking_Error_rate, 0) AS tracking_error_rate,
    COALESCE(c.Gap_Error_rate, 0) AS gap_error_rate,
    COALESCE(c.Destination_Full_rate, 0) AS destination_full_rate,
    COALESCE(c.Destination_Non_Operational_rate, 0) AS destination_non_operational_rate,
    COALESCE(c.Invalid_Destination_rate, 0) AS invalid_destination_rate,
    COALESCE(c.Scanner_Error_rate, 0) AS scan_error_rate,
    COALESCE(c.Destination_Disabled_rate, 0) AS destination_disabled_rate,
    COALESCE(c.Throughput_Limit_rate, 0) AS throughput_limit_rate,
    COALESCE(c.Failed_To_Divert_rate, 0) AS failed_to_divert_rate,
    COALESCE(c.No_Destination_Received_rate, 0) AS no_destination_received_rate,
    COALESCE(c.Lost_Container_rate, 0) AS lost_container_rate,
    COALESCE(c.Dimension_Error_rate, 0) AS dimension_error_rate,
    COALESCE(c.Weight_Error_rate, 0) AS weight_error_rate,
    COALESCE(c.Container_Utilization_rate, 0) AS container_utilization_rate,
    COALESCE(c.Unable_To_Divert_rate, 0) AS unable_to_divert_rate,
    COALESCE(c.Destination_Not_Attempted_rate, 0) AS destination_not_attempted_rate,
    COALESCE(c.total, 0) AS total
FROM (SELECT 1) AS p
LEFT JOIN (
    SELECT
        sLocation_ID,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(COUNT(*) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS total,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 0 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Success_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 1 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Unknown_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 2 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Unexpected_Container_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 3 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Tracking_Error_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 4 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Gap_Error_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 5 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Destination_Full_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 6 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Destination_Non_Operational_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 7 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Invalid_Destination_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 IN (8, 9, 10) THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Scanner_Error_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 12 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Destination_Disabled_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 13 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Throughput_Limit_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 14 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Failed_To_Divert_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 16 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS No_Destination_Received_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 17 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Lost_Container_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 18 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Dimension_Error_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 19 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Weight_Error_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 20 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Container_Utilization_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 21 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Unable_To_Divert_rate,
        CASE WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 22 THEN 1 ELSE 0 END) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2) END AS Destination_Not_Attempted_rate
    FROM item_data
    WHERE t_stamp BETWEEN :starttime AND :endtime
        AND adiSort_Code_0 NOT IN (11, 15)
        AND sLocation_ID IS NOT NULL
        AND sLocation_ID != ''
    GROUP BY sLocation_ID
) AS c ON 1=1
WHERE c.sLocation_ID IS NOT NULL
UNION ALL
SELECT 
    'S01' AS Sorter,
    DATE_FORMAT(:starttime, '%Y-%m-%d %H:%i') AS start_time,
    DATE_FORMAT(:endtime, '%Y-%m-%d %H:%i') AS end_time,
    'N/A' AS sLocation_ID,
    0 AS success_rate,
    0 AS unknown_rate,
    0 AS unexpected_container_rate,
    0 AS tracking_error_rate,
    0 AS gap_error_rate,
    0 AS destination_full_rate,
    0 AS destination_non_operational_rate,
    0 AS invalid_destination_rate,
    0 AS scan_error_rate,
    0 AS destination_disabled_rate,
    0 AS throughput_limit_rate,
    0 AS failed_to_divert_rate,
    0 AS no_destination_received_rate,
    0 AS lost_container_rate,
    0 AS dimension_error_rate,
    0 AS weight_error_rate,
    0 AS container_utilization_rate,
    0 AS unable_to_divert_rate,
    0 AS destination_not_attempted_rate,
    0 AS total
WHERE NOT EXISTS (
    SELECT 1 FROM item_data
    WHERE t_stamp BETWEEN :starttime AND :endtime 
        AND adiSort_Code_0 NOT IN (11, 15)
        AND sLocation_ID IS NOT NULL
        AND sLocation_ID != ''
);
