[英]How to look for missing rows within a group?
Consider the data below. 考虑以下数据。 I am trying to find situations where in a specific RequestID, there is an Attempt: 0
, but Attempt: 2
is missing. 我正在尝试查找在特定RequestID中存在Attempt: 0
,但缺少Attempt: 2
情况。
I've tried looking for Attempt: 0
with a WHERE
predicate and doing NOT EXISTS ... Attempt: 2
in a subquery, but it doesn't return the right data. 我尝试过查找带有WHERE
谓词的Attempt: 0
,并且NOT EXISTS ... Attempt: 2
在子查询中,但是它没有返回正确的数据。
How do I find the RequestIDs with missing Attempt: 2
? 如何找到缺少Attempt: 2
的RequestID?
ID Message RequestID
635828 Attempt: 0 1
635968 Attempt: 1 1
641085 Attempt: 2 1
641230 Attempt: 3 1
643859 Attempt: 0 2
645991 Attempt: 1 2
650255 Attempt: 3 2
652388 Attempt: 0 3
654520 Attempt: 1 3
658785 Attempt: 3 3
You can use not exists
like this: 您可以使用not exists
这样的内容:
select t.*
from t
where t.message = 'Attempt: 0' and
not exists (select 1
from t t2
where t2.requestid = t.requestid and
t2.message = 'Attempt: 2'
);
Another possibility is aggregation: 另一种可能性是聚合:
select requestid
from t
where message in ('Attempt: 0', 'Attempt: 2')
group by requestid
having sum(case when message = 'Attempt: 2' then 1 else 0 end) = 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.