[英]Return all rows that have the same id based on a value on one of the rows of a different column
I would like to return all rows that have the same id on columnA based on a value on one of the rows of a different column say ColumnB.我想根据不同列的行之一上的值返回 columnA 上具有相同 id 的所有行,例如 ColumnB。 The joined table results look like the below, I would like to return all results that do not have at least one instance of the value I am looking for.连接表结果如下所示,我想返回所有没有至少一个我要查找的值实例的结果。
In the example below, I would like to list all organizations that have not been contacted at all which means the organizationname or id which does not have the value 'Contacted' in the ConStatus column.在下面的示例中,我想列出所有根本没有联系过的组织,这意味着在 ConStatus 列中没有值“已联系”的组织名称或 ID。
You can achieve this by using Subquery
您可以通过使用Subquery
来实现这一点
SELECT *
FROM table
WHERE OrgId NOT IN (
SELECT DISTINCT OrgId
FROM table
WHERE ConStatus = 'Contacted'
)
The subquery will get all organization IDs which are contacted.子查询将获取所有联系的组织 ID。 These IDs will then be excluded using NOT IN
to get the ones which are never contacted.然后将使用NOT IN
排除这些 ID,以获得从未联系过的 ID。
You can try using correlated subquery with not exists
您可以尝试使用not exists
相关子查询
select * from tablename a
where not exists
(select 1 from tablename b where a.orgid=b.orgid and ConStatus = 'Contacted')
Try this one.试试这个。
SELECT *
FROM table
WHERE OrgId NOT IN (
SELECT OrgId
FROM table
WHERE ConStatus = 'Contacted'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.