[英]PostgreSQL doesn't remove duplicates
我正在尝试从我的数据库中删除一些重复项。 这是一个包含地图位置数据的表格。某些点已输入两次,我正在尝试将其删除。
这是我使用过的代码
delete from test_table a
using test_table b
where a.location_x = b.location_x and a.location_y = b.location_y and a.type = b.type and a.ctid < b.ctid;
它已经删除了一些重复项。但是我可以在地图上看到仍然有一些重复项。 通过他们的 id 搜索他们确实告诉我他们有相同的位置 x 和 y,但是他们没有被脚本删除。
关于为什么它不起作用的任何想法? 谢谢
你确定他们是平等的? 这些是双精度浮点数, 它们不精确。 尝试检查它们的相等性。
为避免此类问题,请使用任意精度数字或Postgres 的点类型。
我不明白为什么它不起作用,但是,我会这样做:
1)
select a.ctid
from test_table a
where exists
(select 1 test_table b
where a.location_x = b.location_x and a.location_y = b.location_y and a.type = b.type and
a.ctid < b.ctid
)
给出将被删除的 id
和下面的代码删除它们
2)
delete from test_table where ctid in
(
select a.ctid
from test_table a
where exists
(select 1 test_table b
where a.location_x = b.location_x and a.location_y = b.location_y and a.type = b.type and
a.ctid < b.ctid
)
)
您的位置坐标是浮点数。 它们可能在您的数据浏览器未显示的小数位上有所不同。 它们不适合进行平等比较。 通常的做法是测试它们是否“足够接近”。
尝试用abs(a.location_x - b.location_x) < 0.0000001 and abs(a.location_y - b.location_y) < 0.0000001
替换a.location_x = b.location_x and a.location_y = b.location_y
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.