SELECT
    roundtime AS `Round Time`,
    sActual_Dest_ID AS Lane,
    COALESCE(Total_rate, 0) AS Total_rate,
    COALESCE(DestFull_rate, 0) AS DestFull_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 TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
            ELSE ROUND(COUNT(*) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
        END AS Total_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 DestFull_rate
    FROM item_data
    WHERE t_stamp BETWEEN :starttime AND :endtime
        AND (sActual_Dest_ID LIKE CONCAT(:lane, '%') OR :lane IS NULL OR :lane = '')
    GROUP BY roundtime, sActual_Dest_ID
    ORDER BY roundtime ASC
) basa;
