SELECT
    roundtime,
    MCM03_Fluid_Inbound,
    MCM02_Fluid_Inbound,
    Bulk_Inbound
    
FROM (
    SELECT
        FROM_UNIXTIME(CEIL(UNIX_TIMESTAMP(a.eventtime) / 600) * 600) AS roundtime,
        SUM(a.category = 'MCM03_Fluid_Inbound')        AS MCM03_Fluid_Inbound,
        SUM(a.category = 'MCM02_Fluid_Inbound')        AS MCM02_Fluid_Inbound,
        SUM(a.category = 'Bulk_Inbound')        AS Bulk_Inbound
        
    FROM (
        SELECT
            ae.eventtime,
            m.category
        FROM alarm_events ae
        JOIN (
    
        SELECT 'ULC2_2_PE1' AS device, 'Bulk_Inbound' AS category UNION ALL
        SELECT 'ULC3_3_JPE2', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC4_3_JPE2', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC3_7_PE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC3_3_JPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC4_3_JPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC3_6_JPE2', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC4_7_PE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC4_6_JPE2', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC1_8_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC1_8_TPE2', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC1_10_TPE2', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC1_11_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC1_12_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC1_10_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC4_4_JPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC2_8_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC2_11_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC2_12_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC2_10_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC3_6_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC1_4_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC1_4_TPE2', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC2_4_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC2_4_TPE2', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC2_6_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC1_6_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC1_5_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC2_5_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC4_6_TPE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC1_2_PE1', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC2_10_TPE2', 'Bulk_Inbound' UNION ALL
        SELECT 'ULC3_4_JPE1', 'Bulk_Inbound' UNION ALL
        
        SELECT 'PS1_2A_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL3_2_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS2_2_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL6_3_TPE3', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL5_2_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL4_3_TPE3', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS2_1_TPE3', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS2_1_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS2_1_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL5_2_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL6_3_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL6_3_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL4_3_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL4_3_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL3_1_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS2_7_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS2_6_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS2_5A_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL5_1_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL6_7_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL6_6_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL6_5_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL6_5_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL5_6_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL5_5_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL5_4_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL5_4_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL4_7_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL4_6_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS2_6_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL1_5_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL1_5_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL1_6_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS1_6_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS1_4_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS1_3_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS2_8_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS2_1_TPE4', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS1_1_TPE4', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL3_2_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL2_3_TPE3', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL1_3_TPE3', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS1_1_TPE3', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS1_1_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS1_1_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL1_3_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL1_3_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL2_3_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL2_3_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS1_8_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS1_7_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS1_6_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'PS1_3_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL3_6_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL3_5_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL3_4_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL3_4_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL2_7_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL2_6_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL2_5_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL2_5_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL1_7_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL4_5_TPE2', 'MCM02_Fluid_Inbound' UNION ALL
        SELECT 'UL4_5_TPE1', 'MCM02_Fluid_Inbound' UNION ALL
        
        SELECT 'UL11_3_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS3_6_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS3_7_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS3_8_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL12_1_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL7_1_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL12_6_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL12_5_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL12_4_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL12_4_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL11_7_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL11_6_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL11_5_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL11_5_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL10_7_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL10_6_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL10_5_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL10_5_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL9_7_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL9_6_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL9_5_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL9_5_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL8_7_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL8_6_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL8_5_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL8_5_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL7_6_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL7_5_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS3_6_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS3_5A_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS4_4_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS4_5A_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL10_3_TPE3', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS4_2_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS4_1_TPE4', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS3_2_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL12_2_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL11_3_TPE3', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL10_3_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL9_3_TPE3', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL8_3_TPE3', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL7_2_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS4_1_TPE3', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS4_1_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS4_1_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL7_4_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS3_1_TPE3', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS3_1_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL12_2_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL7_2_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL11_3_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL10_3_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL9_3_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL9_3_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL8_3_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL8_3_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS4_8_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS4_7_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS4_6_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS4_6_TPE1', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'PS3_1_TPE2', 'MCM03_Fluid_Inbound' UNION ALL
        SELECT 'UL7_4_TPE1', 'MCM03_Fluid_Inbound'
        ) 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;
