arrays - postgres array_agg 错误 : cannot accumulate arrays of different dimensionality

标签 arrays postgresql aggregate-functions postgis

我在 postgresql 中有一个包裹表,其中 zoning 和 zoning_description 列被 array_agg 转换为文本。 new.universities 表有 9 行,我需要在输出中返回 9 行。

这个查询的目的是找到这些大学所在的所有属性,并将那里的分区类型折叠成 1 个唯一的列,并将它们的几何合并/分解成多边形

select array_agg(distinct dp.zoning) zoning,array_agg(distinct dp.zoning_description) zoning_description,
    uni.school name_,uni.address,'University' type_,1000 buff,st_union(dp.geom) 
from new.universities uni join new.detroit_parcels_update dp
on st_intersects(st_buffer(uni.geom,-10),dp.geom)
group by name_,uni.address,type_,buff

我收到这个错误

ERROR:  cannot accumulate arrays of different dimensionality
********** Error **********

ERROR: cannot accumulate arrays of different dimensionality
SQL state: 2202E

enter image description here

我可以做 array_agg(distinct dp.zoning::text) 分区等。但这会返回一个完全困惑的列,其中包含数组中的嵌套数组... enter image description here

根据这里的答案,我更新的查询不起作用

select array_agg(distinct zoning_u) zoning,array_agg(distinct zoning_description_u) zoning_description,
        uni.school name_,uni.address,'University' type_,1000::int buff,st_union(dp.geom) geom
from new.detroit_parcels_update dp,unnest(zoning) zoning_u,
unnest(zoning_description) zoning_description_u
join new.universities uni
on st_intersects(st_buffer(uni.geom,-10),dp.geom)
group by name_,uni.address,type_,buff order by name_

得到这个错误

ERROR:  invalid reference to FROM-clause entry for table "dp"
LINE 6: on st_intersects(st_buffer(uni.geom,-10),dp.geom)
                                                 ^
HINT:  There is an entry for table "dp", but it cannot be referenced from this part of the query.

********** Error **********

ERROR: invalid reference to FROM-clause entry for table "dp"
SQL state: 42P01
Hint: There is an entry for table "dp", but it cannot be referenced from this part of the query.
Character: 373

我的最终查询是有效的

with t as(select dp.zoning,dp.zoning_description,uni.school name_,uni.address,'University' type_,1000::int buff,st_union(dp.geom) geom
    from new.detroit_parcels_update dp
    join new.universities uni
    on st_intersects(st_buffer(uni.geom,-10),dp.geom)
    group by name_,uni.address,type_,buff,dp.zoning,zoning_description order by name_
    )
select name_,address,type_,buff,st_union(geom) geom,array_agg(distinct z) zoning, array_agg(distinct zd) zoning_description
from t,unnest(zoning) z,unnest(zoning_description) zd 
group by name_,address,type_,buff

最佳答案

示例数据:

create table my_table(name text, numbers text[], letters text[]);
insert into my_table values
    ('first',  '{1, 2}', '{a}'   ),
    ('first',  '{2, 3}', '{a, b}'),
    ('second', '{4}',    '{c, d}'),
    ('second', '{5, 6}', '{c}'   );

您应该聚合数组元素,而不是数组。使用 unnest():

select 
    name, 
    array_agg(distinct number) as numbers, 
    array_agg(distinct letter) as letters
from 
    my_table, 
    unnest(numbers) as number, 
    unnest(letters) as letter
group by name;

  name  | numbers | letters 
--------+---------+---------
 first  | {1,2,3} | {a,b}
 second | {4,5,6} | {c,d}
(2 rows)    

或者,您可以创建自定义聚合。您需要一个函数来合并数组(连接并删除重复项):

create or replace function public.array_merge(arr1 anyarray, arr2 anyarray)
    returns anyarray language sql immutable
as $$
    select array_agg(distinct elem order by elem)
    from (
        select unnest(arr1) elem 
        union
        select unnest(arr2)
    ) s
$$;

create aggregate array_merge_agg(anyarray) (
    sfunc = array_merge,
    stype = anyarray
);

select 
    name, 
    array_merge_agg(numbers) as numbers, 
    array_merge_agg(letters) as letters
from my_table
group by name;

关于arrays - postgres array_agg 错误 : cannot accumulate arrays of different dimensionality,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47319367/

相关文章:

javascript - d3 路径 d = MNaN,NaNLNaN,NaN

mysql - 优化 SQL 查询-传感器读数

sql - 查找重复项,在sql中显示每个结果

javascript - 生成范围 (0 - X) 内的唯一编号,保留历史记录以防止重复

javascript - 在 javascript 中为直方图合并数组

java - 更改变量也会更改先前分配的变量

sql - Postgres pg_trgm GIN 索引在特定连接中被忽略

ios - Vapor /流质 : filtering nested requests

postgresql - 如何连接 Jboss-as-7.1.1 和 Postgresql

postgresql - 用于 postgres 查询的子查询 count()