SELECT
    roundtime,
    Dumpers_jam,
    Inbound_jam,
    Runout_jam,
    Sorter_Recirc_jam,
    Sorter_jam
FROM (
    SELECT
        FROM_UNIXTIME(CEIL(UNIX_TIMESTAMP(a.eventtime) / 600) * 600) AS roundtime,
        SUM(a.category = 'Dumpers_jam')       AS Dumpers_jam,
        SUM(a.category = 'Inbound_jam')       AS Inbound_jam,
        SUM(a.category = 'Runout_jam')        AS Runout_jam,
        SUM(a.category = 'Sorter_Recirc_jam') AS Sorter_Recirc_jam,
        SUM(a.category = 'Sorter_jam')        AS Sorter_jam
    FROM (
        SELECT
            ae.eventtime,
            m.category
        FROM alarm_events ae
        JOIN (
            SELECT 'PS1_1_TPE1' AS device, 'Inbound_jam' AS category UNION ALL
            SELECT 'PS1_1_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'PS1_1_TPE3', 'Inbound_jam' UNION ALL
            SELECT 'PS1_1_TPE4', 'Inbound_jam' UNION ALL
            SELECT 'PS1_2A_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'PS1_3_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'PS1_3_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'PS1_4_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'PS1_6_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'PS1_6_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'PS1_7_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'PS1_8_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'PS2_1_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'PS2_1_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'PS2_1_TPE3', 'Inbound_jam' UNION ALL
            SELECT 'PS2_1_TPE4', 'Inbound_jam' UNION ALL
            SELECT 'PS2_3_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'PS2_3_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'PS2_5_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'PS2_5_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'PS2_6_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'PS2_7_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'PS3_1_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'PS3_1_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'PS3_1_TPE3', 'Inbound_jam' UNION ALL
            SELECT 'PS3_3_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'PS4_1_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'PS4_1_TPE3', 'Inbound_jam' UNION ALL
            SELECT 'PS4_5_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'PS4_5_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL10_3_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL10_3_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL10_6_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL11_3_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL11_3_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL11_7_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL12_2_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL12_2_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL12_7_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL1_1_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL1_2_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL1_2_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL1_4_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL1_4_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL1_5_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL1_6_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL2_3_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL2_3_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL2_3_TPE3', 'Inbound_jam' UNION ALL
            SELECT 'UL2_5_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL2_5_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL2_6_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL2_7_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL2_8_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL3_3_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL3_3_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL3_3_TPE3', 'Inbound_jam' UNION ALL
            SELECT 'UL3_5_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL3_5_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL3_6_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL3_7_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL3_8_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL4_3_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL4_3_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL4_3_TPE3', 'Inbound_jam' UNION ALL
            SELECT 'UL4_5_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL4_5_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL4_6_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL4_7_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL5_3_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL5_3_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL5_3_TPE3', 'Inbound_jam' UNION ALL
            SELECT 'UL5_5_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL5_5_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL5_6_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL5_7_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL5_8_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL6_1_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL6_2_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL6_2_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL6_4_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL6_4_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL6_5_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL6_6_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL6_7_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL7_1_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL7_2_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL7_4_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL7_5_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL7_6_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL8_3_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL8_3_TPE3', 'Inbound_jam' UNION ALL
            SELECT 'UL8_5_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL8_5_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'UL9_5_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL9_7_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'UL9_8_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC1_10_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC1_10_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'ULC1_11_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC1_12_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC1_4_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC1_4_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'ULC1_5_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC1_6_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC1_8_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC1_8_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'ULC2_10_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC2_10_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'ULC2_11_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC2_12_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC2_4_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC2_4_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'ULC2_5_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC2_6_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC2_8_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC2_8_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'ULC3_3_JPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC3_3_JPE2', 'Inbound_jam' UNION ALL
            SELECT 'ULC3_4_JPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC3_6_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC3_6_TPE2', 'Inbound_jam' UNION ALL
            SELECT 'ULC4_3_JPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC4_3_JPE2', 'Inbound_jam' UNION ALL
            SELECT 'ULC4_4_JPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC4_6_TPE1', 'Inbound_jam' UNION ALL
            SELECT 'ULC4_6_TPE2', 'Inbound_jam'
        ) AS m
            ON ae.displaypath = m.device
        WHERE ae.eventtype = 0
            AND ae.eventtime BETWEEN :starttime AND :endtime
            AND ae.displaypath NOT LIKE '%System Startup%'
            AND ae.source NOT LIKE '%System Startup%'
    ) AS a
    GROUP BY FROM_UNIXTIME(CEIL(UNIX_TIMESTAMP(a.eventtime) / 600) * 600)
    ORDER BY roundtime ASC
) AS okeyjam;
