SELECT
    roundtime AS `Round Time`,
    sActual_Dest_ID AS `Lane`,
    COALESCE(Success_rate, 0) AS `Success Rate`,
    COALESCE(Unknown_rate, 0) AS `Unknown Rate`,
    COALESCE(Unexpected_Container_rate, 0) AS `Unexpected Container Rate`,
    COALESCE(Tracking_Error_rate, 0) AS `Tracking Error Rate`,
    COALESCE(Gap_Error_rate, 0) AS `Gap Error Rate`,
    COALESCE(Destination_Full_rate, 0) AS `Destination Full Rate`,
    COALESCE(Destination_Non_Operational_rate, 0) AS `Destination Non Operational Rate`,
    COALESCE(Invalid_Destination_rate, 0) AS `Invalid Destination Rate`,
    COALESCE(Scanner_Error_rate, 0) AS `Scanner Error Rate`,
    COALESCE(Destination_Disabled_rate, 0) AS `Destination Disabled Rate`,
    COALESCE(Throughput_Limit_rate, 0) AS `Throughput Limit Rate`,
    COALESCE(Failed_To_Divert_rate, 0) AS `Failed To Divert Rate`,
    COALESCE(No_Destination_Received_rate, 0) AS `No Destination Received Rate`,
    COALESCE(Lost_Container_rate, 0) AS `Lost Container Rate`,
    COALESCE(Dimension_Error_rate, 0) AS `Dimension Error Rate`,
    COALESCE(Weight_Error_rate, 0) AS `Weight Error Rate`,
    COALESCE(Container_Utilization_rate, 0) AS `Container Utilization Rate`,
    COALESCE(Unable_To_Divert_rate, 0) AS `Unable To Divert Rate`,
    COALESCE(Destination_Not_Attempted_rate, 0) AS `Destination Not Attempted Rate`
FROM (
    SELECT
        FROM_UNIXTIME(
            FLOOR(UNIX_TIMESTAMP(t_stamp) / 
                CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0)
            ) * 
            CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0)
        ) AS roundtime,
        sActual_Dest_ID,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 0 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Success_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 1 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Unknown_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 2 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Unexpected_Container_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 3 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Tracking_Error_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 4 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Gap_Error_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 5 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Destination_Full_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 6 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Destination_Non_Operational_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 7 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Invalid_Destination_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 IN (8, 9, 10) THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Scanner_Error_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 12 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Destination_Disabled_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 13 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Throughput_Limit_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 14 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Failed_To_Divert_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 16 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS No_Destination_Received_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 17 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Lost_Container_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 18 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Dimension_Error_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 19 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Weight_Error_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 20 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Container_Utilization_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 21 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 2) END AS Unable_To_Divert_rate,
        CASE WHEN CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0) = 0 THEN 0 ELSE ROUND(SUM(CASE WHEN adiSort_Code_0 = 22 THEN 1 ELSE 0 END) * 3600.0 / CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0), 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 (COALESCE(:Lane, '') = '' OR sActual_Dest_ID = :Lane)
    GROUP BY roundtime, sActual_Dest_ID
    ORDER BY roundtime ASC
) basa;

