I'm trying filter out the duplicates of the color column, but only for each group of parent_id , not the entire color column itself.
For example, I have the following table:
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 |
The result I am looking for is this:
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 |
Note how there is only one blue for parent_id = 5
, and one blue for parent_id = 6
. The same is true for the rest of the colors.
Any help on this is much appreciated.
select parent_id, min(child_id), color
from your_tab
group by parent_id, color;
If you want to delete them:
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;
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.