SELECT
    roundtime AS `Round Time`,
    sInduction_Name AS `Induction Name`,
    CONCAT(COALESCE(total_single_carrier, 0), '%') AS `Total Single Carrier`,
    CONCAT(COALESCE(total_double_carrier, 0), '%') AS `Total Double Carrier`
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,
        CASE 
            WHEN (
                COALESCE(SUM(diTotal_Single_Carrier), 0) +
                COALESCE(SUM(diTotal_Double_Carrier), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diTotal_Single_Carrier), 0) * 100.0) / (
                COALESCE(SUM(diTotal_Single_Carrier), 0) +
                COALESCE(SUM(diTotal_Double_Carrier), 0)
            ), 2)
        END AS total_single_carrier,
        CASE 
            WHEN (
                COALESCE(SUM(diTotal_Single_Carrier), 0) +
                COALESCE(SUM(diTotal_Double_Carrier), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diTotal_Double_Carrier), 0) * 100.0) / (
                COALESCE(SUM(diTotal_Single_Carrier), 0) +
                COALESCE(SUM(diTotal_Double_Carrier), 0)
            ), 2)
        END AS total_double_carrier
    FROM induction_data
    WHERE t_stamp BETWEEN :starttime AND :endtime
        AND (COALESCE(:inductionname, '') = '' OR sInduction_Name = :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,
        0 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 (COALESCE(:inductionname, '') = '' OR :inductionname = 'S02')
    GROUP BY roundtime
    ORDER BY roundtime ASC
) basa;

