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`,
    'S03aa' AS `Scanner Name`,

    -- REAL SCAN STATS FROM item_data
    SUM(CASE WHEN adiSort_Code_0 NOT IN (0, 8, 9, 10, 11, 15) THEN 1 ELSE 0 END) AS `Total Bad Reads`,
    SUM(CASE WHEN adiSort_Code_0 = 0 THEN 1 ELSE 0 END) AS `Total Good Reads`,
    SUM(CASE WHEN adiSort_Code_0 = 10 THEN 1 ELSE 0 END) AS `Total Multi Reads`,
    SUM(CASE WHEN adiSort_Code_0 = 9 THEN 1 ELSE 0 END) AS `Total No Data`,
    SUM(CASE WHEN adiSort_Code_0 = 8 THEN 1 ELSE 0 END) AS `Total No Reads`,

    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 'S03%'
    AND (:scannername IS NULL OR :scannername = '' OR :scannername = 'S03aa')

GROUP BY DATE(t_stamp), HOUR(t_stamp)
ORDER BY `Start Timestamp` ASC;
