Can't aggregate arrays

蹲街弑〆低调 提交于 2019-11-30 05:05:12

问题


I can create an array of arrays:

select array[array[1, 2], array[3, 4]];
     array     
---------------
 {{1,2},{3,4}}

But I can't aggregated arrays:

select array_agg(array[c1, c2])
from (
    values (1, 2), (3, 4)
) s(c1, c2);
ERROR:  could not find array type for data type integer[]

What am I missing?


回答1:


I use:

CREATE AGGREGATE array_agg_mult(anyarray) (
    SFUNC = array_cat,
    STYPE = anyarray,
    INITCOND = '{}'
);

and queries like:

SELECT array_agg_mult( ARRAY[[x,x]] ) FROM generate_series(1,10) x;

Note that you must aggregate 2-dimensional arrays, so you'll often want to wrap an input array in a single-element ARRAY[array_to_aggregate] array constructor.



来源:https://stackoverflow.com/questions/15947943/cant-aggregate-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!