SELECT 
  SUBSTRING_INDEX(SUBSTRING_INDEX(strValue, '/', 2), '/', -1) AS Location,
  CASE priority
    WHEN 0 THEN 'Diagnostic'
    WHEN 1 THEN 'Low'
    WHEN 2 THEN 'Medium'
    WHEN 3 THEN 'High'
    WHEN 4 THEN 'Critical'
    ELSE 'Unknown'
  END AS Priority,
  COUNT(*) AS Count
FROM (
    SELECT 
      ae.id,
      ae.eventid,
      ae.priority,
      aed.strValue
    FROM alarm_events ae
    LEFT JOIN alarm_event_data aed 
      ON ae.id = aed.id 
     AND aed.propname = 'myTag'
    WHERE ae.eventtype = 0
      AND NOT EXISTS (
          SELECT 1 
          FROM alarm_events ae_clear
          WHERE ae_clear.eventid = ae.eventid
            AND ae_clear.eventtype = 1
      )
      AND ae.displaypath NOT LIKE '%System Startup%'
      AND ae.source NOT LIKE '%System Startup%'
) AS Active
GROUP BY Location, Priority
ORDER BY Location, Priority;
