PostgreSQL:无法对某些数据类型使用 DISTINCT

发布于 2024-09-07 08:38:13 字数 430 浏览 3 评论 0原文

我有一个名为 _sample_table_delme_data_files 的表,其中包含一些重复项。我想将其记录(不重复)复制到 data_files 中:

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 类型。我如何提供这样的排序运算符,以便我只能将不同的值放入我的目标表中?

预先感谢,

亚当

I have a table called _sample_table_delme_data_files which contains some duplicates. I want to copy its records, without duplicates, into data_files:

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.

Problem is, PostgreSQL can not compare (or order) box3d types. How do I supply such an ordering operator so I can get only the distinct into my destination table?

Thanks in advance,

Adam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

数理化全能战士 2024-09-14 08:38:13

如果您不添加运算符,您可以尝试使用其输出函数将 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,所以它未经测试。)

If you don't add the operator, you could try translating the box3d data to text using its output function, something like:

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

Edit The next step is: cast it back to box3d:

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

(I don't have box3d on my system so it's untested.)

掩于岁月 2024-09-14 08:38:13

数据类型 box3d 没有用于 DISTINCT 操作的运算符。您必须创建运算符,或者询问PostGIS-项目,也许有人已经解决了这个问题。

The datatype box3d doesn't have an operator for the DISTINCT-operation. You have to create the operator, or ask the PostGIS-project, maybe somebody has already fixed this problem.

濫情▎り 2024-09-14 08:38:13

最后被同事解决了。

让我们看看有多少重复项:

SELECT COUNT(*) FROM _sample_table_delme_data_files ;
 count                                                               
-------                                                              
 12728                                                               
(1 row)

现在,我们将在源表中添加另一列,以帮助我们区分相似的行:

ALTER TABLE _sample_table_delme_data_files ADD COLUMN id2 serial;

我们现在可以看到重复项:

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

Finally, this was solved by a colleague.

Let's see how many dups are there:

SELECT COUNT(*) FROM _sample_table_delme_data_files ;
 count                                                               
-------                                                              
 12728                                                               
(1 row)

Now, we shall add another column to the source table to help us differentiate similar rows:

ALTER TABLE _sample_table_delme_data_files ADD COLUMN id2 serial;

We can now see the dups:

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)       

And remove them:

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);

Let's see it worked:

SELECT id FROM _sample_table_delme_data_files GROUP BY id HAVING COUNT(*)>1;
 id
----
(0 rows)

Remove the auxiliary column:

ALTER TABLE _sample_table_delme_data_files DROP COLUMN id2;
ALTER TABLE

Insert the remaining rows into the destination table:

INSERT INTO data_files (SELECT * FROM _sample_table_delme_data_files);
INSERT 0 6364
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文