[英]Only return rows if all items with the same ID has all the same value in another column
I am trying to figure out a way to identify all ID's that only contain all of the same value in another column. 我正在尝试找出一种方法来识别所有ID,该ID在另一列中仅包含所有相同的值。
In the example above Looking for all SubID's that are inactive it would only return rows for C2 (ID's 2, 5, & 6). 在上面的示例中,查找所有不活动的SubID只会返回C2的行(ID为2、5和6)。
Sample Data : 样本数据 :
use group by and sub-query 使用分组依据和子查询
select t.* from
(select subid,status from t t1
group by subid,status
having count(*)>1
) as t1
inner join t on t.subid=t1.subid and t.status=t1.status
You use not exists
: 您使用not exists
:
select t.*
from table t
where not exists (select 1 from table t1 where t1.subid = t.subid and t1.status = 'Active');
EDIT : If you want to get the subid
s which have same status then you can do : 编辑:如果要获取具有相同状态的subid
,则可以执行以下操作:
select t.*
from table t
where not exists (select 1 from table t1 where t1.subid = t.subid and t1.status <> t.status);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.