SELECT
    roundtime AS `Round Time`,
    sInduction_Name AS `Induction Name`,
    COALESCE(total_single_carrier, 0) AS `Total Single Carrier`,
    COALESCE(total_double_carrier, 0) AS `Total Double Carrier`,
    COALESCE(total_single_carrier, 0) + 
    COALESCE(total_double_carrier, 0) AS `Total`
FROM (
    SELECT
        FROM_UNIXTIME(
            FLOOR(UNIX_TIMESTAMP(t_stamp) / 
                CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0)
            ) * 
            CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0)
        ) AS roundtime,
        sInduction_Name,
        SUM(diTotal_Single_Carrier) AS total_single_carrier,
        SUM(diTotal_Double_Carrier) AS total_double_carrier
    FROM induction_data
    WHERE t_stamp BETWEEN :starttime AND :endtime
        AND (sInduction_Name = :inductionname OR :inductionname IS NULL OR :inductionname = '')
    GROUP BY roundtime, sInduction_Name
    
    UNION ALL
    
    SELECT
        FROM_UNIXTIME(
            FLOOR(UNIX_TIMESTAMP(t_stamp) / 
                CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0)
            ) * 
            CEIL(TIMESTAMPDIFF(SECOND, :starttime, :endtime) / 24.0)
        ) AS roundtime,
        'S02' AS sInduction_Name,
        0 AS total_single_carrier,
        COUNT(*) AS total_double_carrier
    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 roundtime
    ORDER BY roundtime ASC
) basa;

