select count DISTINCT where!= 2 filter

时间:2015-12-24 04:34:51

标签: mysql sql

我的表格有 eventid (警报事件ID)和事件类型(active = 1, Ack = 2, unack = 0)。我想展示一些未经确认的警报。

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

enter image description here

不确定我如何过滤掉已被确认的黄昏

3 个答案:

答案 0 :(得分:2)

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

或等效

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

请参阅Return row only if value doesn't exist

答案 1 :(得分:0)

SELECT count( DISTINCT eventid) FROM alarm_events where eventtype in (1,0)

试试这个

答案 2 :(得分:0)

您可以使用分组依据进行操作,对于过滤,您可以使用IN运算符。

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

如果您的eventtype列是String,则使用('0','1')替换(0,1)。