简体   繁体   中英

How to find rows where column A is seen with multiple values of column B (and vice versa)

Given the following schema, I'd like to return rows where A or B appear in more than one row that is not a single pairing.

For example:

fruitl + fruitU should be the only combination seen in the table. Eg apple + Apple is okay, but if we see apple + APple or Apple + Apple then there is an issue with data quality.

Essentially A + B should be a unique combo.

 fruitl | fruitu | ...
----------------------
 apple  | Apple  | ...
 apple  | Apple  | ...
 apple  | APple  | ...
 banana | Banana | ...
 banana | Banana | ...
 peach  | Peach  | ...
 peach  | Peach  | ...

From the above table the query would show...

apple | APple

or

apple | Apple

since apple is paired with two unique values.

I have been approaching it using something like the below but that only seems to return distinct values.

There are an unlimited number of potential fruits in the database.

select *
from table
group by fruitl, fruitu
having count(*) > 1;

Hmmm, I think you want something like this:

select t.*
from table t
where (A in ('a', 'b') or B in ('a', 'b')) and not (A = 'a' and B = 'b');

I'm not sure if you also want not (A = 'b' and B = 'a') . The question is unclear about this combination.

There are other ways to express the logic, but this seems to most closely match your description.

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