简体   繁体   English

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

[英]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.

相关问题 如何按一列分组并限制为另一列对组中所有行具有相同值的行? - How to group by one column and limit to rows where another column has the same value for all rows in group? 仅当具有相同第 1 列的所有行中的第 2 列为 NULL 时才返回第 1 列 - Return column 1 only if column 2 is NULL in all rows with the same column 1 Select 在另一列中具有相同 id 和相同值的所有行,而不使用 have 子句 - Select all the rows with same id and same value in another column without using having clause 返回具有相同 id 和单独列的行都包含相同的值 - Returning rows with the same id and separate column all containing same value SQL-如果一列中所有具有相同“订单号”的行在另一列中具有相同的“插槽”,则返回一个值 - SQL - Return a value if all rows that have the same “order number” in one column have the same “slot” in another 如何获取所有具有相同ID但其他列中的值不同的行? - How to get all rows that has same ID but diffrent value in other column? MySQL:仅返回一个表中的行,其中另一个表的一列中的所有值都相同 - MySQL: Return only rows in one table where ALL values in one column of another table are the same 查询以针对一列的每个不同值返回在所有行中具有相同值的行值 - Query to return row values which has same values in all the rows for each different value of one column 按 ID 分组,仅在另一列具有相同值的情况下 - Group by ID, only where another column has the same value 过滤所有行的特定条件,并为具有相同值的每一列仅返回一行 - Filtering down specific criteria for all rows and return only one row for each column with the same value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM