Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 如何删除某些列上重复的行?_Postgresql_Duplicates - Fatal编程技术网

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   |