我正在尝试过滤掉颜色列的副本,但仅针对每个 parent_id 组,而不是整个颜色列本身。
例如,我有下表:
parent_id | child_id | color |
5 | 1 | blue |
5 | 2 | blue |
5 | 3 | green |
5 | 4 | green |
5 | 5 | yellow |
5 | 6 | orange |
6 | 7 | blue |
6 | 8 | blue |
6 | 9 | magenta |
6 | 10 | green |
6 | 11 | magenta |
6 | 12 | orange |
我正在寻找的结果是:
parent_id | child_id | color |
5 | 1 | blue |
5 | 3 | green |
5 | 5 | yellow |
5 | 6 | orange |
6 | 7 | blue |
6 | 9 | magenta |
6 | 10 | green |
6 | 12 | orange |
请注意parent_id = 5
只有一个蓝色,parent_id = 6
只有一个蓝色。其余的颜色也是如此。
非常感谢任何帮助。
答案 0 :(得分:2)
select parent_id, min(child_id), color
from your_tab
group by parent_id, color;
答案 1 :(得分:0)
如果你想删除它们:
delete t
from table t left join
(select parent_id, color, min(child_id) as minci
from table t
group by parent_id, color
) pc
on t.parent_id = pc.parent_id and t.color = pc.color and t.child_id = pc.minci
where t.parent_id is null;