我的表格有 eventid (警报事件ID)和事件类型(active = 1, Ack = 2, unack = 0
)。我想展示一些未经确认的警报。
SELECT count( DISTINCT eventid) FROM alarm_events where eventtype != 2
不确定我如何过滤掉已被确认的黄昏
答案 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
答案 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)。