PostgreSQL:Can';对于某些数据类型,不能使用DISTINCT

PostgreSQL:Can';对于某些数据类型,不能使用DISTINCT,postgresql,postgis,comparator,Postgresql,Postgis,Comparator,我有一个名为\u sample\u table\u delme\u data\u files的表,其中包含一些重复项。我想将其记录复制到数据\u文件中,无重复项: INSERT INTO data_files (SELECT distinct * FROM _sample_table_delme_data_files); ERROR: could not identify an ordering operator for type box3d HINT: Use an explicit or

我有一个名为
\u sample\u table\u delme\u data\u files
的表,其中包含一些重复项。我想将其记录复制到
数据\u文件中,无重复项

INSERT INTO data_files (SELECT distinct * FROM _sample_table_delme_data_files);
ERROR:  could not identify an ordering operator for type box3d
HINT:  Use an explicit ordering operator or modify the query.
问题是,PostgreSQL无法比较(或排序)
box3d
类型。如何提供这样一个排序运算符,以便只将distinct输入目标表

提前感谢,


Adam

数据类型box3d没有用于不同操作的运算符。您必须这样做,或者询问PostGIS项目,可能有人已经解决了此问题。

如果不添加运算符,您可以尝试使用其输出功能将
box3d
数据转换为文本,例如:

INSERT INTO data_files (SELECT distinct othercols,box3dout(box3dcol) FROM _sample_table_delme_data_files);
编辑下一步是:将其转换回
box3d

INSERT INTO data_files SELECT othercols, box3din(b) FROM (SELECT distinct othercols,box3dout(box3dcol) AS b FROM _sample_table_delme_data_files);

(我的系统上没有
box3d
,因此未经测试。)

最后,一位同事解决了这个问题

让我们看看有多少DUP:

SELECT COUNT(*) FROM _sample_table_delme_data_files ;
 count                                                               
-------                                                              
 12728                                                               
(1 row)
现在,我们将向源表中添加另一列,以帮助区分类似的行:

ALTER TABLE _sample_table_delme_data_files ADD COLUMN id2 serial;
我们现在可以看到DUP:

SELECT id, id2 FROM _sample_table_delme_data_files ORDER BY id LIMIT 10;
   id   | id2                                                                           
--------+------                                                                         
 198748 | 6449                                                                          
 198748 |   85                                                                          
 198801 |  166                                                                          
 198801 | 6530                                                                          
 198829 |   87                                                                          
 198829 | 6451                                                                          
 198926 |   88                                                                          
 198926 | 6452                                                                          
 199062 | 6532                                                                          
 199062 |  168                                                                          
(10 rows)       
并删除它们:

DELETE FROM _sample_table_delme_data_files 
    WHERE id2 IN (SELECT max(id2) FROM _sample_table_delme_data_files 
                         GROUP BY id 
                               HAVING COUNT(*)>1);
让我们看看它是否奏效:

SELECT id FROM _sample_table_delme_data_files GROUP BY id HAVING COUNT(*)>1;
 id
----
(0 rows)
拆下辅助柱:

ALTER TABLE _sample_table_delme_data_files DROP COLUMN id2;
ALTER TABLE
将其余行插入目标表:

INSERT INTO data_files (SELECT * FROM _sample_table_delme_data_files);
INSERT 0 6364

所以实际上你只需要区分“id”列而不是所有列(包括几何列)?如果你在问题中解释的话,答案会有很大的不同。你是对的,这就是为什么我把@Edmund的答案标记为正确答案的原因;我希望这也能有所帮助。