SELECT
    'S01' AS Sorter,
    DATE_FORMAT(:starttime, '%Y-%m-%d %H:%i') AS start_time,
    DATE_FORMAT(:endtime, '%Y-%m-%d %H:%i') AS end_time,
    COALESCE(c.sScanner_Name, 'N/A') AS sScanner_Name,
    CONCAT(COALESCE(c.total_bad_reads, 0), '%') AS total_bad_reads,
    CONCAT(COALESCE(c.total_comm_faults, 0), '%') AS total_comm_faults,
    CONCAT(COALESCE(c.total_good_reads, 0), '%') AS total_good_reads,
    CONCAT(COALESCE(c.total_multi_items, 0), '%') AS total_multi_items,
    CONCAT(COALESCE(c.total_multi_reads, 0), '%') AS total_multi_reads,
    CONCAT(COALESCE(c.total_no_data, 0), '%') AS total_no_data,
    CONCAT(COALESCE(c.total_no_reads, 0), '%') AS total_no_reads,
    COALESCE(c.total, 0) AS total
FROM (SELECT 1) AS p
LEFT JOIN (
    SELECT
        sScanner_Name,
        CASE 
            WHEN (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diScanner_bad_reads), 0) * 100.0) / (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ), 2)
        END AS total_bad_reads,
        CASE 
            WHEN (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diScanner_comm_fault), 0) * 100.0) / (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ), 2)
        END AS total_comm_faults,
        CASE 
            WHEN (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diScanner_good_reads), 0) * 100.0) / (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ), 2)
        END AS total_good_reads,
        CASE 
            WHEN (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diScanner_multi_items), 0) * 100.0) / (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ), 2)
        END AS total_multi_items,
        CASE 
            WHEN (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diScanner_multi_reads), 0) * 100.0) / (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ), 2)
        END AS total_multi_reads,
        CASE 
            WHEN (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diScanner_no_data), 0) * 100.0) / (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ), 2)
        END AS total_no_data,
        CASE 
            WHEN (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ) = 0 THEN 0
            ELSE ROUND((COALESCE(SUM(diScanner_no_reads), 0) * 100.0) / (
                COALESCE(SUM(diScanner_bad_reads), 0) +
                COALESCE(SUM(diScanner_comm_fault), 0) +
                COALESCE(SUM(diScanner_good_reads), 0) +
                COALESCE(SUM(diScanner_multi_items), 0) +
                COALESCE(SUM(diScanner_multi_reads), 0) +
                COALESCE(SUM(diScanner_no_data), 0) +
                COALESCE(SUM(diScanner_no_reads), 0)
            ), 2)
        END AS total_no_reads,
        (
            COALESCE(SUM(diScanner_bad_reads), 0) +
            COALESCE(SUM(diScanner_comm_fault), 0) +
            COALESCE(SUM(diScanner_good_reads), 0) +
            COALESCE(SUM(diScanner_multi_items), 0) +
            COALESCE(SUM(diScanner_multi_reads), 0) +
            COALESCE(SUM(diScanner_no_data), 0) +
            COALESCE(SUM(diScanner_no_reads), 0)
        ) AS total
    FROM scanner_reads
    WHERE t_stamp BETWEEN :starttime AND :endtime
    GROUP BY sScanner_Name
) AS c ON 1=1

UNION ALL

SELECT
    'S02' AS Sorter,
    DATE_FORMAT(:starttime, '%Y-%m-%d %H:%i') AS start_time,
    DATE_FORMAT(:endtime, '%Y-%m-%d %H:%i') AS end_time,
    'S02aa' AS sScanner_Name,
    CONCAT(COALESCE(sorter_total.total_bad_reads, 0), '%') AS total_bad_reads,
    '0%' AS total_comm_faults,
    CONCAT(COALESCE(sorter_total.total_good_reads, 0), '%') AS total_good_reads,
    '0%' AS total_multi_items,
    CONCAT(COALESCE(sorter_total.total_multi_reads, 0), '%') AS total_multi_reads,
    CONCAT(COALESCE(sorter_total.total_no_data, 0), '%') AS total_no_data,
    CONCAT(COALESCE(sorter_total.total_no_reads, 0), '%') AS total_no_reads,
    COALESCE(sorter_total.total, 0) AS total
FROM (SELECT 1) AS p
LEFT JOIN (
    SELECT 
        CASE 
            WHEN COUNT(*) = 0 THEN 0
            ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 NOT IN (0, 8, 9, 10, 11, 15) THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2)
        END AS total_bad_reads,
        CASE 
            WHEN COUNT(*) = 0 THEN 0
            ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 0 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2)
        END AS total_good_reads,
        CASE 
            WHEN COUNT(*) = 0 THEN 0
            ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 10 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2)
        END AS total_multi_reads,
        CASE 
            WHEN COUNT(*) = 0 THEN 0
            ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 9 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2)
        END AS total_no_data,
        CASE 
            WHEN COUNT(*) = 0 THEN 0
            ELSE ROUND((SUM(CASE WHEN adiSort_Code_0 = 8 THEN 1 ELSE 0 END) * 100.0) / COUNT(*), 2)
        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 'S02%'
) AS sorter_total ON 1=1;
