我有一个使用带有 distinct 作为参数的 array_agg 的查询,并且在 postgres 9.6 上不被接受。
我创建了这个示例来说明这个问题:
create table numbers (id integer primary key, name varchar(10));
insert into numbers values(1,'one');
insert into numbers values(2,'two');
postgres 9.4
select array_agg(distinct(id)) from numbers;
array_agg
-----------
{1,2}
postgres 9.6
ERROR: function array_agg(integer) is not unique
LINE 1: select array_agg(distinct(id)) from numbers;
^
HINT: Could not choose a best candidate function.
You might need to add explicit type casts.
为了在 postgres 9.6 上得到这个结果,我需要改变什么?
谢谢。
这就是我检查功能的结果:
nspname | proname | proargtypes
------------+-----------+---------------------
pg_catalog | array_agg | [0:0]={anyarray}
public | array_agg | [0:0]={anyelement}
pg_catalog | array_agg | [0:0]={anynonarray
现在,由于pozs的评论,我发现了这个问题。我删除了聚合函数的公共定义并且它起作用了。
问题出在我正在处理的数据库上,因为我发现有些人说该示例对他们有用,所以我创建了一个新数据库并运行该示例。然后唯一的变化是聚合函数定义。