繁体   English   中英

仅当具有相同ID的所有项目在另一列中都具有相同的值时,才返回行

[英]Only return rows if all items with the same ID has all the same value in another column

我正在尝试找出一种方法来识别所有ID,该ID在另一列中仅包含所有相同的值。

在上面的示例中,查找所有不活动的SubID只会返回C2的行(ID为2、5和6)。

样本数据 :

在此处输入图片说明

使用分组依据和子查询

  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

您使用not exists

select t.*
from table t
where not exists (select 1 from table t1 where t1.subid = t.subid and t1.status = 'Active');

编辑:如果要获取具有相同状态的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM