0

I have a table that has eventid (alarm Event id) and eventtype (active = 1, Ack = 2, unack = 0). I would like to show a count of unacknowledged alarms.

SELECT count( DISTINCT eventid) FROM alarm_events where eventtype != 2

enter image description here

Not sure how i filter out the eventid that has been acknowledged

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Allen Ray
  • 1
  • 1

3 Answers3

2
SELECT COUNT(DISTINCT eventid)
FROM alarm_events
WHERE eventid NOT IN (
    SELECT eventid
    FROM alarm_events
    WHERE eventtype = 2)

or equivalently

SELECT COUNT(DISTINCT a.eventid)
FROM alarm_events AS a
LEFT JOIN alarm_events AS b ON a.eventid = b.eventid AND b.eventtype = 2
WHERE b.eventid IS NULL

See Return row only if value doesn't exist

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
0
SELECT count( DISTINCT eventid) FROM alarm_events where eventtype in (1,0)

try this

Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
0

You can do it using Group By and for filtering you can use IN operator.

SELECT count(eventid) FROM alarm_events WHERE eventtype in (0, 1) GROUP BY eventid

Pl replace (0, 1) with ('0','1') if your eventtype column is String.

Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41