简体   繁体   中英

How can I select all the rows which do not share a column value with another row which is null?

I know that title is word vomit, but I wasn't sure how to say this more concisely.

I have a very large table with columns A and B. This table is supposed to have an unusual property: for every value of column A, there should be at least one row where the value of B is null. However, this table has some anomalous rows which do NOT abide by this property and I would like to identify them so that I can clean up the table.

id  A   B
----------
1   c   e
2   c   f
3   c   NULL
4   d   e
5   d   f
6   d   g

In this example, the rows where column A is c satisfy the criteria because row 3's B value is NULL. However, the rows where column A is d do not satisfy the criteria because there is no row where column A is d and column B is null. I would like to run a query which returns d.

you can try this

SELECT DISTINCT A from Test where A not in (
select A from Test where B is null );

You could also use aggregation:

select d
from t
group by d
having count(*) = count(b);

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