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.sInduction_Name, 'N/A') AS sInduction_Name,
    COALESCE(data.SingleCarrier_count, 0) AS SingleCarrier_count,
    COALESCE(data.DoubleCarrier_count, 0) AS DoubleCarrier_count,
    COALESCE(data.SingleCarrier_count, 0) + COALESCE(data.DoubleCarrier_count, 0) AS Total_count
FROM (SELECT 1) AS p
LEFT JOIN (
    SELECT 
        CONCAT(DATE(t_stamp), ' ', HOUR(t_stamp), ':00') AS roundtime,
        sInduction_Name,
        SUM(diTotal_Single_Carrier) AS SingleCarrier_count,
        SUM(diTotal_Double_Carrier) AS DoubleCarrier_count
    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 roundtime,
        'S03' AS sInduction_Name,
        COUNT(*) AS SingleCarrier_count,
        0 AS DoubleCarrier_count
    FROM item_data
    WHERE t_stamp BETWEEN :starttime AND :endtime
        AND adiSort_Code_0 NOT IN (11, 15)
        AND sLocation_ID LIKE 'S03%'
        AND (:inductionname IS NULL OR :inductionname = '' OR :inductionname = 'S03')
    GROUP BY DATE(t_stamp), HOUR(t_stamp)
) AS data ON 1=1
ORDER BY data.roundtime ASC;

