Question

I have a DB with 3 tables:

Alarm

ID          Message
-------------------
1           Server01 Down
2           Switch01 Port 2 down
3           Webserver Down

ListAlarmStates

ID       StateName
------------------
1        Raised
2        RaisedNotified
3        Cleared
4        ClearedNotified
5        ForceClear

AlarmStates

ID       AlarmId        ListAlarmStatesId
-----------------------------------------
1        1              1
2        1              2
3        1              3
4        1              4
5        2              1
6        2              5
7        3              1

Now I would like to know all alarms that don't have the status ClearedNotified but do have the status cleared (the status cleared I could catch in code)

Thanks in advance!

Was it helpful?

Solution

SELECT AlarmId FROM
AlarmStates AS
INNER JOIN Alarm A
 ON (AS.AlarmID = A.ID)
INNER JOIN ListAlarmStates LA
 ON ( AS.ListAlarmStatesId = LA.ID)
GROUP BY AS.AlarmID
HAVING COUNT(CASE WHEN LA.StateName = 'ClearedNotified' THEN 1 ELSE NULL END) = 0
   AND COUNT(CASE WHEN LA.StateName = 'Cleared' THEN 1 ELSE NULL END) > 0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top