Postgresql 如何删除某些列上重复的行?
我有下表:Postgresql 如何删除某些列上重复的行?,postgresql,duplicates,Postgresql,Duplicates,我有下表: Id color size 1 blue medium 2 blue medium 3 blue small 4 red big 我想排除所有颜色和大小重复但Id不同的行。但请记住,我不想保留其中一行,我想排除颜色和大小相似但Id不同的两行。这是我的最后一张表: Id color size 3 blue small 4 red big 我找到了这篇文章和这篇
Id color size
1 blue medium
2 blue medium
3 blue small
4 red big
我想排除所有颜色和大小重复但Id不同的行。但请记住,我不想保留其中一行,我想排除颜色和大小相似但Id不同的两行。这是我的最后一张表:
Id color size
3 blue small
4 red big
我找到了这篇文章和这篇文章。但他们都想保留其中一行。我不想要它
有什么办法吗?不存在:
如果要从表中删除重复的行,请执行以下操作:
delete from tablename t
where exists (
select 1 from tablename
where id <> t.id and color = t.color and size = t.size
);
你确定结果中要2,蓝色,中等?3,红色,中等来自哪里?它不是原件data@a_horse_with_no_name你完全正确,很抱歉。只是fixed@forpas是的,我想要。这是3号,红色,大的
select t.Id, t.color, t.size
from (
select *, count(*) over (partition by color, size) counter
from tablename
) t
where t.counter = 1
delete from tablename t
where exists (
select 1 from tablename
where id <> t.id and color = t.color and size = t.size
);
| id | color | size |
| --- | ----- | ----- |
| 3 | blue | small |
| 4 | red | big |