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,
    COALESCE(c.total_bad_reads, 0) AS total_bad_reads,
    COALESCE(c.total_comm_faults, 0) AS total_comm_faults,
    COALESCE(c.total_good_reads, 0) AS total_good_reads,
    COALESCE(c.total_multi_items, 0) AS total_multi_items,
    COALESCE(c.total_multi_reads, 0) AS total_multi_reads,
    COALESCE(c.total_no_data, 0) AS total_no_data,
    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 TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
            ELSE ROUND(COALESCE(SUM(diScanner_bad_reads), 0) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
        END AS total_bad_reads,
        CASE 
            WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
            ELSE ROUND(COALESCE(SUM(diScanner_comm_fault), 0) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
        END AS total_comm_faults,
        CASE 
            WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
            ELSE ROUND(COALESCE(SUM(diScanner_good_reads), 0) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
        END AS total_good_reads,
        CASE 
            WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
            ELSE ROUND(COALESCE(SUM(diScanner_multi_items), 0) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
        END AS total_multi_items,
        CASE 
            WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
            ELSE ROUND(COALESCE(SUM(diScanner_multi_reads), 0) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
        END AS total_multi_reads,
        CASE 
            WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
            ELSE ROUND(COALESCE(SUM(diScanner_no_data), 0) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
        END AS total_no_data,
        CASE 
            WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
            ELSE ROUND(COALESCE(SUM(diScanner_no_reads), 0) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
        END AS total_no_reads,
        CASE 
            WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
            ELSE ROUND((
                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)
            ) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
        END 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,
    CASE 
        WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
        ELSE ROUND(COALESCE(sorter_total.total_bad_reads, 0) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
    END AS total_bad_reads,
    0 AS total_comm_faults,
    CASE 
        WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
        ELSE ROUND(COALESCE(sorter_total.total_good_reads, 0) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
    END AS total_good_reads,
    0 AS total_multi_items,
    CASE 
        WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
        ELSE ROUND(COALESCE(sorter_total.total_multi_reads, 0) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
    END AS total_multi_reads,
    CASE 
        WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
        ELSE ROUND(COALESCE(sorter_total.total_no_data, 0) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
    END AS total_no_data,
    CASE 
        WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
        ELSE ROUND(COALESCE(sorter_total.total_no_reads, 0) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
    END AS total_no_reads,
    CASE 
        WHEN TIMESTAMPDIFF(SECOND, :starttime, :endtime) = 0 THEN 0
        ELSE ROUND(COALESCE(sorter_total.total, 0) * 3600.0 / TIMESTAMPDIFF(SECOND, :starttime, :endtime), 2)
    END AS total
FROM (SELECT 1) AS p
LEFT JOIN (
    SELECT 
        SUM(CASE WHEN adiSort_Code_0 NOT IN (0, 8, 9, 10, 11, 15) THEN 1 ELSE 0 END) AS total_bad_reads,
        SUM(CASE WHEN adiSort_Code_0 = 0 THEN 1 ELSE 0 END) AS total_good_reads,
        SUM(CASE WHEN adiSort_Code_0 = 10 THEN 1 ELSE 0 END) AS total_multi_reads,
        SUM(CASE WHEN adiSort_Code_0 = 9 THEN 1 ELSE 0 END) AS total_no_data,
        SUM(CASE WHEN adiSort_Code_0 = 8 THEN 1 ELSE 0 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;
