SELECT
    COALESCE(data.roundtime, 'N/A') AS Startstamp,
    COALESCE(CONCAT('H', TIMESTAMPDIFF(HOUR, DATE_FORMAT(data.roundtime, "%Y-%m-%d %H:00:00"), DATE_FORMAT(NOW(), "%Y-%m-%d %H:00:00"))), 'N/A') AS Hour,
    COALESCE(data.total_count, 0) AS total_count,
    COALESCE(data.success_count, 0) AS success_count,
    COALESCE(data.unknown_count, 0) AS unknown_count,
    COALESCE(data.unexpected_container_count, 0) AS unexpected_container_count,
    COALESCE(data.tracking_error_count, 0) AS tracking_error_count,
    COALESCE(data.gap_error_count, 0) AS gap_error_count,
    COALESCE(data.destination_full_count, 0) AS destination_full_count,
    COALESCE(data.destination_non_operational_count, 0) AS destination_non_operational_count,
    COALESCE(data.invalid_destination_count, 0) AS invalid_destination_count,
    COALESCE(data.destination_disabled_count, 0) AS destination_disabled_count,
    COALESCE(data.throughput_limit_count, 0) AS throughput_limit_count,
    COALESCE(data.failed_to_divert_count, 0) AS failed_to_divert_count,
    COALESCE(data.no_destination_received_count, 0) AS no_destination_received_count,
    COALESCE(data.lost_container_count, 0) AS lost_container_count,
    COALESCE(data.dimension_error_count, 0) AS dimension_error_count,
    COALESCE(data.weight_error_count, 0) AS weight_error_count,
    COALESCE(data.container_utilization_count, 0) AS container_utilization_count,
    COALESCE(data.unable_to_divert_count, 0) AS unable_to_divert_count,
    COALESCE(data.destination_not_attempted_count, 0) AS destination_not_attempted_count,
    COALESCE(data.scan_error_count, 0) AS scan_error_count
FROM (SELECT 1) AS p
LEFT JOIN (
    SELECT
        CONCAT(DATE(t_stamp), ' ', LPAD(HOUR(t_stamp), 2, '0'), ':00') AS roundtime,
        COUNT(*) AS total_count,
        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 = 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,
        SUM(CASE WHEN adiSort_Code_0 IN (8, 9, 10) THEN 1 ELSE 0 END) AS scan_error_count
    FROM item_data
    WHERE sActual_Dest_ID = :lane 
        AND t_stamp BETWEEN :starttime AND :endtime
        AND adiSort_Code_0 NOT IN (11, 15)
    GROUP BY DATE(t_stamp), HOUR(t_stamp)
) AS data ON 1=1
ORDER BY data.roundtime ASC;

