我有一个查询,使用了带有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的评论,找到了问题所在。我删除了聚合函数的公共定义,问题得以解决。
问题只出现在我正在使用的数据库上。我发现有些人说示例对他们有效,于是我创建了一个新的数据库并运行了示例。然后唯一的变化就是聚合函数的定义。
distinct
不是一个函数。distinct id
和distinct (id)
是一样的。 - user330315array_agg
命名函数来检查,例如使用此链接:http://rextester.com/MLHSEL68822。 - pozs------------+-----------+--------------------- pg_catalog | array_agg | [0:0]={anyarray} public | array_agg | [0:0]={anyelement} pg_catalog | array_agg | [0:0]={anynonarray} - Browser_80