SELECT
    'S03' AS Sorter,
    DATE_FORMAT(:starttime, '%Y-%m-%d %H:%i') AS start_time,
    DATE_FORMAT(:endtime, '%Y-%m-%d %H:%i') AS end_time,
    'S03aa' AS sScanner_Name,

    COALESCE(sorter_total.total_bad_reads, 0) AS total_bad_reads,
    0 AS total_comm_faults,
    COALESCE(sorter_total.total_good_reads, 0) AS total_good_reads,
    0 AS total_multi_items,
    COALESCE(sorter_total.total_multi_reads, 0) AS total_multi_reads,
    COALESCE(sorter_total.total_no_data, 0) AS total_no_data,
    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 
        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 'S03%'
) AS sorter_total ON 1 = 1;
