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`,
    sInduction_Name AS `Induction Name`,
    COALESCE(SUM(diTotal_Single_Carrier), 0) AS `Total Single Carrier`,
    COALESCE(SUM(diTotal_Double_Carrier), 0) AS `Total Double Carrier`,
    COALESCE(SUM(diTotal_Single_Carrier), 0) + COALESCE(SUM(diTotal_Double_Carrier), 0) AS `Total`
FROM induction_data
WHERE t_stamp BETWEEN :starttime AND :endtime
    AND (sInduction_Name = :inductionname OR :inductionname IS NULL OR :inductionname = '')
GROUP BY DATE(t_stamp), HOUR(t_stamp), sInduction_Name

UNION ALL

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`,
    'S02' AS `Induction Name`,
    COUNT(*) AS `Total Single Carrier`,
    0 AS `Total Double Carrier`,
    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 (:inductionname IS NULL OR :inductionname = '' OR :inductionname = 'S02')
GROUP BY DATE(t_stamp), HOUR(t_stamp)
ORDER BY `Start Timestamp` ASC;

