SELECT 
    COALESCE(data.roundtime, 'N/A') AS StartTimestamp,
    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.sScanner_Name, 'N/A') AS sScanner_Name,
    COALESCE(data.BadReads, '0%') AS BadReads,
    COALESCE(data.CommFaults, '0%') AS CommFaults,
    COALESCE(data.GoodReads, '0%') AS GoodReads,
    COALESCE(data.MultiItems, '0%') AS MultiItems,
    COALESCE(data.MultiReads, '0%') AS MultiReads,
    COALESCE(data.NoData, '0%') AS NoData,
    COALESCE(data.NoReads, '0%') AS NoReads,
    COALESCE(data.Total, 0) AS Total
FROM (SELECT 1) AS p
LEFT JOIN (
    SELECT 
        CONCAT(DATE(t_stamp), ' ', HOUR(t_stamp), ':00') AS roundtime,
        sScanner_Name,
        CONCAT(
            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 BadReads,
        CONCAT(
            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 CommFaults,
        CONCAT(
            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 GoodReads,
        CONCAT(
            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 MultiItems,
        CONCAT(
            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 MultiReads,
        CONCAT(
            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 NoData,
        CONCAT(
            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 NoReads,
        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) AS Total
    FROM scanner_reads
    WHERE t_stamp BETWEEN :starttime AND :endtime
        AND (sScanner_Name = :scannername OR :scannername IS NULL OR :scannername = '')
    GROUP BY DATE(t_stamp), HOUR(t_stamp), sScanner_Name
    
    UNION ALL
    
    SELECT 
        CONCAT(DATE(t_stamp), ' ', HOUR(t_stamp), ':00') AS roundtime,
        'S02aa' AS sScanner_Name,
        CONCAT(
            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 BadReads,
        '0%' AS CommFaults,
        CONCAT(
            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 GoodReads,
        '0%' AS MultiItems,
        CONCAT(
            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 MultiReads,
        CONCAT(
            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 NoData,
        CONCAT(
            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 NoReads,
        COUNT(*) AS Total
    FROM item_data
    WHERE t_stamp BETWEEN :starttime AND :endtime
        AND adiSort_Code_0 NOT IN (11, 15)
        AND sLocation_ID LIKE 'S02%'
        AND (:scannername IS NULL OR :scannername = '' OR :scannername = 'S02aa')
    GROUP BY DATE(t_stamp), HOUR(t_stamp)
) AS data ON 1=1
ORDER BY data.roundtime ASC;
