SELECT
    roundtime AS `Round Time`,
    sScanner_Name AS `Scanner Name`,
    CONCAT(COALESCE(total_bad_reads, 0), '%') AS `Total Bad Reads`,
    CONCAT(COALESCE(total_comm_faults, 0), '%') AS `Total Comm Faults`,
    CONCAT(COALESCE(total_good_reads, 0), '%') AS `Total Good Reads`,
    CONCAT(COALESCE(total_multi_items, 0), '%') AS `Total Multi Items`,
    CONCAT(COALESCE(total_multi_reads, 0), '%') AS `Total Multi Reads`,
    CONCAT(COALESCE(total_no_data, 0), '%') AS `Total No Data`,
    CONCAT(COALESCE(total_no_reads, 0), '%') AS `Total No Reads`
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,
        sScanner_Name,
        CASE 
            WHEN (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diScanner_bad_reads), 0) * 100.0) / (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ), 2)
        END AS total_bad_reads,
        CASE 
            WHEN (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diScanner_comm_fault), 0) * 100.0) / (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ), 2)
        END AS total_comm_faults,
        CASE 
            WHEN (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diScanner_good_reads), 0) * 100.0) / (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ), 2)
        END AS total_good_reads,
        CASE 
            WHEN (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diScanner_multi_items), 0) * 100.0) / (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ), 2)
        END AS total_multi_items,
        CASE 
            WHEN (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diScanner_multi_reads), 0) * 100.0) / (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ), 2)
        END AS total_multi_reads,
        CASE 
            WHEN (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diScanner_no_data), 0) * 100.0) / (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ), 2)
        END AS total_no_data,
        CASE 
            WHEN (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diScanner_no_reads), 0) * 100.0) / (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ), 2)
        END AS total_no_reads
    FROM scanner_reads
    WHERE t_stamp BETWEEN :starttime AND :endtime
        AND (COALESCE(:scannername, '') = '' OR sScanner_Name = :scannername)
    GROUP BY roundtime, sScanner_Name
    
    UNION ALL
    
    SELECT
        FROM_UNIXTIME(
            FLOOR(UNIX_TIMESTAMP(t_stamp) / 
                CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0)
            ) * 
            CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0)
        ) AS roundtime,
        'S02aa' AS sScanner_Name,
        CASE 
            WHEN COUNT(*) = 0 THEN 0
            ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 NOT IN (0, 8, 9, 10, 11, 15) THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2)
        END AS total_bad_reads,
        0 AS total_comm_faults,
        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 total_good_reads,
        0 AS total_multi_items,
        CASE 
            WHEN COUNT(*) = 0 THEN 0
            ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 10 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2)
        END AS total_multi_reads,
        CASE 
            WHEN COUNT(*) = 0 THEN 0
            ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 9 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2)
        END AS total_no_data,
        CASE 
            WHEN COUNT(*) = 0 THEN 0
            ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 8 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2)
        END AS total_no_reads
    FROM item_data
    WHERE t_stamp BETWEEN :starttime AND :endtime
        AND adiSort_Code_0 NOT IN (11, 15)
        AND sLocation_ID LIKE 'S02%'
        AND (COALESCE(:scannername, '') = '' OR :scannername = 'S02aa')
    GROUP BY roundtime
    ORDER BY roundtime ASC
) basa;

