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. 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.
You can achieve this by using 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. These IDs will then be excluded using NOT IN
to get the ones which are never contacted.
You can try using correlated subquery with 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'
)
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.