简体   繁体   中英

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.

In the example above Looking for all SubID's that are inactive it would only return rows for C2 (ID's 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 :

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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