我有一个返回下表的查询。
父母 | 子女 | 孙子女 | 性别||
---|---|---|---|---|
1 | A | A | xx||
1 | B | B>xy | ||
1 | C | xy | ||
1 | A | dxy|||
1 | D | e | xy | |
1 | E | f | >xx | |
2 | F | g | xx
您需要在此处添加一个自联接作为附加条件(例如,使用WHERE NOT EXISTS,如上面注释中所建议的(。
看看如何使用自联接在SQL中删除重复项,这应该会帮助您了解:https://flylib.com/books/en/2.305.1/eliminating_duplicates_from_a_self_join_result.html
我认为您希望父/子不重复。您可以使用窗口功能:
select t.*
from (select t.*,
count(*) over (partition by parent, child) as cnt
from t
) t
where cnt = 1 and gender = 'xx'