-- Hourly Lane Details Rate Query (Table Display)
-- Shows every hour in detail with all lane statistics as rates (items per hour)
-- Rate calculation matches LaneDetails_Rate.sql formula
-- Filtered by specific lane parameter

SELECT
    CONCAT(DATE(Startstamp), ' ', LPAD(HOUR(Startstamp), 2, '0'), ':00') AS StartTimestamp,
    CONCAT('H', TIMESTAMPDIFF(HOUR,
        DATE_FORMAT(Startstamp, "%Y-%m-%d %H:00:00"),
        DATE_FORMAT(NOW(), "%Y-%m-%d %H:00:00")
    )) AS Hour,
    CONCAT(ROUND(total_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS total_rate,
    CONCAT(ROUND(success_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS success_rate,
    CONCAT(ROUND(unknown_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS unknown_rate,
    CONCAT(ROUND(unexpected_container_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS unexpected_container_rate,
    CONCAT(ROUND(tracking_error_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS tracking_error_rate,
    CONCAT(ROUND(gap_error_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS gap_error_rate,
    CONCAT(ROUND(destination_full_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS destination_full_rate,
    CONCAT(ROUND(destination_non_operational_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS destination_non_operational_rate,
    CONCAT(ROUND(invalid_destination_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS invalid_destination_rate,
    CONCAT(ROUND(destination_disabled_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS destination_disabled_rate,
    CONCAT(ROUND(throughput_limit_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS throughput_limit_rate,
    CONCAT(ROUND(failed_to_divert_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS failed_to_divert_rate,
    CONCAT(ROUND(no_destination_received_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS no_destination_received_rate,
    CONCAT(ROUND(lost_container_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS lost_container_rate,
    CONCAT(ROUND(dimension_error_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS dimension_error_rate,
    CONCAT(ROUND(weight_error_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS weight_error_rate,
    CONCAT(ROUND(container_utilization_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS container_utilization_rate,
    CONCAT(ROUND(unable_to_divert_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS unable_to_divert_rate,
    CONCAT(ROUND(destination_not_attempted_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS destination_not_attempted_rate,
    CONCAT(ROUND(scan_error_count * 3600 / TIMESTAMPDIFF(SECOND, Startstamp, Endtstamp), 2), ' pph') AS scan_error_rate
FROM (
    SELECT
        DATE(t_stamp) AS date_part,
        HOUR(t_stamp) AS hour_part,
        MIN(t_stamp) AS Startstamp,
        MAX(t_stamp) AS Endtstamp,
        COUNT(*) AS total_count,
        SUM(DivertStatus = 0) AS success_count,
        SUM(DivertStatus = 1) AS unknown_count,
        SUM(DivertStatus = 2) AS unexpected_container_count,
        SUM(DivertStatus = 3) AS tracking_error_count,
        SUM(DivertStatus = 4) AS gap_error_count,
        SUM(DivertStatus = 5) AS destination_full_count,
        SUM(DivertStatus = 6) AS destination_non_operational_count,
        SUM(DivertStatus = 7) AS invalid_destination_count,
        SUM(DivertStatus = 12) AS destination_disabled_count,
        SUM(DivertStatus = 13) AS throughput_limit_count,
        SUM(DivertStatus = 14) AS failed_to_divert_count,
        SUM(DivertStatus = 16) AS no_destination_received_count,
        SUM(DivertStatus = 17) AS lost_container_count,
        SUM(DivertStatus = 18) AS dimension_error_count,
        SUM(DivertStatus = 19) AS weight_error_count,
        SUM(DivertStatus = 20) AS container_utilization_count,
        SUM(DivertStatus = 21) AS unable_to_divert_count,
        SUM(DivertStatus = 22) AS destination_not_attempted_count,
        SUM(DivertStatus IN (8, 9, 10)) AS scan_error_count
    FROM alltable
    WHERE DEST_REQ = :lane AND t_stamp BETWEEN :starttime AND :endtime
    GROUP BY DATE(t_stamp), HOUR(t_stamp)
) counts
ORDER BY Startstamp ASC;
