SELECT
    roundtime AS `Round Time`,
    sLocation_ID AS `Location ID`,
    CONCAT(COALESCE(Success_pct, 0), '%') AS `Success Percentage`,
    CONCAT(COALESCE(Unknown_pct, 0), '%') AS `Unknown Percentage`,
    CONCAT(COALESCE(Unexpected_Container_pct, 0), '%') AS `Unexpected Container Percentage`,
    CONCAT(COALESCE(Tracking_Error_pct, 0), '%') AS `Tracking Error Percentage`,
    CONCAT(COALESCE(Gap_Error_pct, 0), '%') AS `Gap Error Percentage`,
    CONCAT(COALESCE(Destination_Full_pct, 0), '%') AS `Destination Full Percentage`,
    CONCAT(COALESCE(Destination_Non_Operational_pct, 0), '%') AS `Destination Non Operational Percentage`,
    CONCAT(COALESCE(Invalid_Destination_pct, 0), '%') AS `Invalid Destination Percentage`,
    CONCAT(COALESCE(Scanner_Error_pct, 0), '%') AS `Scanner Error Percentage`,
    CONCAT(COALESCE(Destination_Disabled_pct, 0), '%') AS `Destination Disabled Percentage`,
    CONCAT(COALESCE(Throughput_Limit_pct, 0), '%') AS `Throughput Limit Percentage`,
    CONCAT(COALESCE(Failed_To_Divert_pct, 0), '%') AS `Failed To Divert Percentage`,
    CONCAT(COALESCE(No_Destination_Received_pct, 0), '%') AS `No Destination Received Percentage`,
    CONCAT(COALESCE(Lost_Container_pct, 0), '%') AS `Lost Container Percentage`,
    CONCAT(COALESCE(Dimension_Error_pct, 0), '%') AS `Dimension Error Percentage`,
    CONCAT(COALESCE(Weight_Error_pct, 0), '%') AS `Weight Error Percentage`,
    CONCAT(COALESCE(Container_Utilization_pct, 0), '%') AS `Container Utilization Percentage`,
    CONCAT(COALESCE(Unable_To_Divert_pct, 0), '%') AS `Unable To Divert Percentage`,
    CONCAT(COALESCE(Destination_Not_Attempted_pct, 0), '%') AS `Destination Not Attempted Percentage`
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,
        sLocation_ID,
        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)
        AND (COALESCE(:locationid, '') = '' OR sLocation_ID = :locationid)
    GROUP BY roundtime, sLocation_ID
    ORDER BY roundtime ASC
) basa;

