postgres array_agg错误:无法累积不同维度的数组

时间:2017-11-16 00:00:39

标签: arrays postgresql postgis

我在postgresql中有一个parcels表,其中分区和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

2 个答案:

答案 0 :(得分:8)

示例数据:

private void AddErrors(IdentityResult result)
        {
            foreach (var error in result.Errors)
            {
                //ModelState.AddModelError("", error);
                ModelState.AddModelError("", "PASSWORDS MUST BE AT LEAST 6 CHARACTERS LONG, WITH AT LEAST ONE NUMBER, AND ONE NON LETTER OR DIGIT SUCH AS %, #, @, !, AND *. AN EXAMPLE: PASSWORD1$.");
            }

您应该聚合数组元素,而不是数组。使用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)    

答案 1 :(得分:0)

很多simpler alternative是创建自定义聚合函数(您只需要做一次)

CREATE AGGREGATE array_concat_agg(anyarray) (
   SFUNC = array_cat,
   STYPE = anyarray
);

然后将 array_agg 替换为 array_concat_agg

SELECT
    array_concat_agg(DISTINCT dp.zoning) zoning,
    array_concat_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