6

我有一个使用带有 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的评论,我发现了这个问题。我删除了聚合函数的公共定义并且它起作用了。

问题出在我正在处理的数据库上,因为我发现有些人说该示例对他们有用,所以我创建了一个新数据库并运行该示例。然后唯一的变化是聚合函数定义。

4

2 回答 2

7

现在,由于 pozs 的评论,我发现了这个问题。我删除了聚合函数的公共定义并且它起作用了。

问题出在我正在处理的数据库上,因为我发现有些人说该示例对他们有用,所以我创建了一个新数据库并运行该示例。然后唯一的变化是聚合函数定义。

所以我放弃了函数 public | array_agg | [0:0]={anyelement} 并且有效。

非常感谢。

于 2017-04-20T17:28:41.183 回答
0

它的工作原理与 x86_64-pc-linux-gnu 上的 PostgreSQL 9.6.2 上的 dbfiddle所展示的完全一样,由 gcc (Debian 4.9.2-10) 4.9.2, 64-bit 编译。

在此处输入图像描述

于 2017-04-20T16:43:41.610 回答