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,
        (
            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;
