SELECT 
    CONCAT(DATE(t_stamp), ' ', HOUR(t_stamp), ':00') AS `Start Timestamp`,
    CONCAT('H', TIMESTAMPDIFF(HOUR, DATE_FORMAT(t_stamp, "%Y-%m-%d %H:00:00"), DATE_FORMAT(NOW(), "%Y-%m-%d %H:00:00"))) AS `Hour`,
    sScanner_Name AS `Scanner Name`,
    COALESCE(SUM(diScanner_bad_reads), 0) AS `Total Bad Reads`,
    COALESCE(SUM(diScanner_comm_fault), 0) AS `Total Comm Faults`,
    COALESCE(SUM(diScanner_good_reads), 0) AS `Total Good Reads`,
    COALESCE(SUM(diScanner_multi_items), 0) AS `Total Multi Items`,
    COALESCE(SUM(diScanner_multi_reads), 0) AS `Total Multi Reads`,
    COALESCE(SUM(diScanner_no_data), 0) AS `Total No Data`,
    COALESCE(SUM(diScanner_no_reads), 0) AS `Total No Reads`,
    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
ORDER BY DATE(t_stamp) ASC, HOUR(t_stamp) ASC;

