SELECT
  base.Total_Jam,
  base.Dumpers_jam,
  base.Inbound_jam,
  base.Runout_jam,
  base.Sorter_Recirc_jam,
  base.Startstamp,
  base.Endtstamp,
  base.Sorter_jam,
  1 AS Total_perc,
  base.Dumpers_jam       / NULLIF(base.Total_Jam, 0) AS Dumpers_perc,
  base.Inbound_jam       / NULLIF(base.Total_Jam, 0) AS Inbound_perc,
  base.Runout_jam        / NULLIF(base.Total_Jam, 0) AS Runout_perc,
  base.Sorter_Recirc_jam / NULLIF(base.Total_Jam, 0) AS Sorter_Recirc_perc,
  base.Sorter_jam        / NULLIF(base.Total_Jam, 0) AS Sorter_perc
FROM (
  SELECT
    MIN(a.eventtime)                                   AS Startstamp,
    MAX(COALESCE(c.eventtime, a.eventtime))            AS Endtstamp,
    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,
    SUM(a.category IN ('Dumpers_jam','Inbound_jam','Runout_jam','Sorter_Recirc_jam','Sorter_jam')) AS Total_Jam
  FROM (
    SELECT
      ae.id,
      ae.eventtime,
      ae.eventid,
      ae.displaypath AS device,
      m.category
    FROM alarm_events ae
    JOIN (
      /* ---------- Inbound_jam (MCM01, MCM02, MCM03, MCM06, MCM07) ---------- */
      SELECT DISTINCT device, category FROM (
        SELECT 'UL16_3_TPE2' AS device, 'Inbound_jam' AS category UNION ALL
      SELECT 'UL18_14_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS6_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL16_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL20_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_8_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL21_16_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL20_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_14_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_13_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL19_1_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_4_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL14_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL16_1_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL21_11_TPE5', 'Inbound_jam' UNION ALL
      SELECT 'UL17_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL20_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL17_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_3_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL14_3_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS5_12_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL20_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_4_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL13_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL15_3_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS6_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL15_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL17_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_8_TPE5', 'Inbound_jam' UNION ALL
      SELECT 'UL14_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL20_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL17_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_8_TPE4', 'Inbound_jam' UNION ALL
      SELECT 'UL18_12_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL16_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL14_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_12_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL16_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_12_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS7_14_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL21_11_TPE4', 'Inbound_jam' UNION ALL
      SELECT 'UL16_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_10_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS5_13_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_12_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL14_7_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL18_10_TPE4', 'Inbound_jam' UNION ALL
      SELECT 'UL21_15_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL20_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_12_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS7_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL17_7_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL15_6_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL15_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL20_3_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS7_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_8_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS5_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL17_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_11_TPE3', 'Inbound_jam' UNION ALL
      SELECT 'UL15_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_10_TPE3', 'Inbound_jam' UNION ALL
      SELECT 'UL16_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS5_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL18_15_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL17_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL15_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL13_11_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_11_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL15_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_8_TPE3', 'Inbound_jam' UNION ALL
      SELECT 'PS6_10_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL20_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_11_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_4_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL18_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_3_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL21_13_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_10_TPE5', 'Inbound_jam' UNION ALL
      SELECT 'UL21_15_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_8_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS5_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL16_6_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL20_6_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL14_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL14_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL15_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_6_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL16_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_12_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_10_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL19_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_1_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_7_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS7_14_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_10_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS7_12_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_4_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL14_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL15_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL16_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_8_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL21_13_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL18_12_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL19_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL14_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_10_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL20_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_8_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL13_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_16_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL20_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_15_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL13_10_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL15_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_11_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL17_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_15_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_12_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL20_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_12_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL16_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_10_TPE3', 'Inbound_jam' UNION ALL
      SELECT 'UL18_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_4_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL13_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_11_TPE3', 'Inbound_jam' UNION ALL
      SELECT 'PS5_8_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS5_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_10_TPE5', 'Inbound_jam' UNION ALL
      SELECT 'UL13_1_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL15_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL17_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL16_1_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_13_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_12_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL17_7_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL20_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL16_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_14_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL15_3_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL21_11_TPE5', 'Inbound_jam' UNION ALL
      SELECT 'UL13_8_TPE5', 'Inbound_jam' UNION ALL
      SELECT 'PS5_12_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS5_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS7_12_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_8_TPE3', 'Inbound_jam' UNION ALL
      SELECT 'UL20_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL15_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL17_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL20_6_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL13_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL20_3_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL14_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL15_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_10_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL17_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL16_6_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL21_11_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_13_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL18_3_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL15_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL17_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL16_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL17_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_12_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS7_14_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_6_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL13_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_8_TPE4', 'Inbound_jam' UNION ALL
      SELECT 'UL18_10_TPE4', 'Inbound_jam' UNION ALL
      SELECT 'PS7_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL16_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS6_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_15_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL14_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL16_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_14_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS7_4_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL15_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_7_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL14_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL14_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL14_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_12_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL14_7_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL18_14_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_11_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS7_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL14_3_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL20_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL20_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL16_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL15_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_1_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_11_TPE4', 'Inbound_jam' UNION ALL
      SELECT 'UL21_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL16_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_10_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS5_4_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL13_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_3_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'PS7_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL19_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'PS6_4_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL16_3_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL15_6_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL13_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL11_15_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_3_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL13_13_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_5_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_1_TPE4', 'Inbound_jam' UNION ALL
      SELECT 'UL4_19_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL13_18_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL7_16_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_2_TPE4', 'Inbound_jam' UNION ALL
      SELECT 'UL4_15_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL4_17_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL1_19_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL1_14_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL7_19_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL11_17_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_23_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL21_20_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL1_17_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_22_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_3_TPE5', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL7_15_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_18_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_3_TPE3', 'Inbound_jam' UNION ALL
      SELECT 'UL11_16_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_3_TPE4', 'Inbound_jam' UNION ALL
      SELECT 'UL7_14_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_14_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_15_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL4_14_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_18_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_18_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL11_19_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_1_TPE3', 'Inbound_jam' UNION ALL
      SELECT 'UL11_14_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_19_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_20_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_1_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL1_19_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_2_TPE5', 'Inbound_jam' UNION ALL
      SELECT 'UL7_19_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL7_17_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL1_15_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL1_16_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_1_TPE5', 'Inbound_jam' UNION ALL
      SELECT 'UL11_19_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'UL21_19_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_22_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL13_16_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_3_TPE7', 'Inbound_jam' UNION ALL
      SELECT 'UL21_21_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL18_17_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL21_23_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL4_16_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_2_TPE3', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'UL4_19_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_3_TPE6', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_1_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'NCS1_4_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'NCP_11_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCS2_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_15_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'NCS1_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'S02_1_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_21_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'S02_2CH_JPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_13_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_19_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'NCS1_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_12_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCS2_4_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'NCS2_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_17_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_19_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP_11_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_21_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'NCS2_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCS2_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'NCS1_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCS1_1_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCS1_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCS1_6_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'NCS2_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCS1_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCS2_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'S02_1CH_JPE1', 'Inbound_jam' UNION ALL
      SELECT 'S02_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_15_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCS1_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'NCP1_17_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_12_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYCA_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDA_8_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYDA_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCB_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCA_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCB_5_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYCD_12_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAC_11_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_15_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCB_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_15_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_12_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAC_7_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYAC_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBD_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAC_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBC_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYDC_13_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_15_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAD_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_14_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDC_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_10_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_13_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_8_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_13_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCB_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBD_9_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYDC_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBD_12_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYCA_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAC_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCB_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBD_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBC_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBD_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDA_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAC_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_15_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCB_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_4_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_4_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYAD_5_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYCB_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBD_14_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBC_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDA_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDC_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDC_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCA_5_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYCD_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCD_4_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_14_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCD_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDA_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBD_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_13_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAD_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAC_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAC_11_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCD_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDC_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCA_9_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYBD_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_6_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYCD_7_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYDA_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCD_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_13_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYCD_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAD_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDC_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAD_7_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYCA_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDC_11_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBC_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCB_7_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYBC_8_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYDA_6_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_13_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCD_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYDC_13_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_6_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYBC_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBD_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCA_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBC_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBC_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_16_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_11_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBC_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAD_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCD_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDA_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBD_12_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDC_8_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYAD_2_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDC_6_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYBD_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_10_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYCD_11_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDA_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYDC_6_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAD_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAC_5_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_11_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYCD_10_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCA_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBD_13_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAD_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYBD_4_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAC_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYCA_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_9_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBD_3_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYAB_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYAC_8_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBC_6_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYDC_2_TPE2', 'Inbound_jam' UNION ALL
      SELECT 'BYDB_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCB_5_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYCB_7_TPE1', 'Inbound_jam' UNION ALL
      SELECT 'BYBA_8_TPE1', 'Inbound_jam'
      ) AS device_list

      UNION ALL
      /* ---------- Dumpers_jam (MCM04, MCM05) ---------- */
      SELECT DISTINCT device, category FROM (
        SELECT 'PS11_1_JPE1' AS device, 'Dumpers_jam' AS category UNION ALL
      SELECT 'PS11_11CH1_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_1_JPE4', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_11_JPE5', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_6_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PRS3_5_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_11_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_8_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PRS3_3A_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_11CH5_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_5CH4_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_1_JPE3', 'Dumpers_jam' UNION ALL
      SELECT 'ULC7_3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PRS3_5_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_5CH1_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_4_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_1_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_5_JPE4', 'Dumpers_jam' UNION ALL
      SELECT 'ULC5_3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_11CH4_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_6_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_5_JPE3', 'Dumpers_jam' UNION ALL
      SELECT 'ULC8_3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PRS4_2_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_2_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_5_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PRS3_3B_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_11CH3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PRS3_3A_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_5_JPE6', 'Dumpers_jam' UNION ALL
      SELECT 'PRS4_1_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_9_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_5_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'ULC7_3_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PRS4_2_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PRS3_3B_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_1_JPE3', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_1_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_5CH2_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_11_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_11_JPE6', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_1_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_11CH6_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_11_JPE3', 'Dumpers_jam' UNION ALL
      SELECT 'PRS3_2A_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_1_JPE4', 'Dumpers_jam' UNION ALL
      SELECT 'ULC6_3_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_2_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'ULC6_3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'ULC5_3_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_5_JPE5', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_11_JPE7', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_11CH2_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_11_JPE4', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_5CH3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_3_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'ULC8_3_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS11_7_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PRS3_2B_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS10_3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS9_3CH3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PRS1_2A_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PRS1_6_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PRS1_4_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_8_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS9_3CH1_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PRS1_2B_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_11_CH2_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_1_JPE4', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_11_JPE4', 'Dumpers_jam' UNION ALL
      SELECT 'PS9_2_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'ULC3_3_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'ULC4_3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_11_CH3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_2_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'ULC4_3_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_5_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_4_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'ULC2_3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PRS1_6_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'ULC2_3_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_8_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PRS1_3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_1_JPE6', 'Dumpers_jam' UNION ALL
      SELECT 'PRS1_3_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_11_JPE3', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_11_CH4_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'ULC1_3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_1_JPE5', 'Dumpers_jam' UNION ALL
      SELECT 'PS9_3CH2_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_11_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'ULC3_3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS9_2_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS9_3_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS9_3_JPE3', 'Dumpers_jam' UNION ALL
      SELECT 'PS9_3_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PRS1_2B_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS9_3CH4_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PRS1_4_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_11_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PRS2_1_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_1_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_11_CH1_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_9_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PRS1_2A_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_2_JPE1', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_1_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'ULC1_3_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_4_JPE2', 'Dumpers_jam' UNION ALL
      SELECT 'PS8_1_JPE3', 'Dumpers_jam'
      ) AS dumpers_list

      UNION ALL
      /* ---------- Runout_jam (placeholder) ---------- */
      SELECT DISTINCT device, category FROM (
        SELECT 'RUN1_1_TPE1' AS device, 'Runout_jam' AS category UNION ALL
        SELECT 'RUN1_2_TPE1', 'Runout_jam'
      ) AS runout_list
    ) 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%'
    GROUP BY ae.id, ae.eventtime, ae.eventid, ae.displaypath, m.category
  ) AS a
  LEFT JOIN (
    SELECT eventid, MIN(eventtime) AS eventtime
    FROM alarm_events
    WHERE eventtype = 1
    GROUP BY eventid
  ) AS c
    ON a.eventid = c.eventid
) AS base;
