Re: array_agg problem

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: "TJ O'Donnell" <tjo(at)acm(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: array_agg problem
Date: 2011-08-19 21:44:31
Message-ID: CAHyXU0wkN2a02uckbUM0VmE=1=Pd-08VLN=O1H_yJBtXFL_cHA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Aug 19, 2011 at 4:22 PM, TJ O'Donnell <tjo(at)acm(dot)org> wrote:
> array_agg says it can take any type of arg, but this seems not to work
> when the arg in an integer array.
>
> create temp table x(name text, val integer[]);
> insert into x values('a',array[1,2,3]);
> insert into x values('b',array[3,4,5]);
> select * from x;
> select max(val), min(val) from x;
> select array_agg(val) from x;
>
> Well, everything works except the last statement.  In 8.4 I get
> ERROR:  could not find array type for data type integer[]
>
> I was hoping for [1,2,3,4,5] or at least [1,2,3,3,4,5] or even [[1,2,3],[3,4,5]]
> Am I not understanding something?

yeah basically -- it's a common misunderstanding. array_agg converts
elements to arrays, but not arrays to array of dimension + 1. you
could do this:

select array(select unnest(val) from x);

to get [1,2,3,3,4,5].

Getting [[1,2,3],[3,4,5]] is a little harder:
create aggregate array_stack(int[])
(
sfunc=array_cat,
stype=int[]
);

postgres=# select array_stack(array[val]) from x;
array_stack
-------------------
{{1,2,3},{3,4,5}}
(1 row)

merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2011-08-19 22:11:41 Re: A questions on planner choices
Previous Message TJ O'Donnell 2011-08-19 21:22:53 array_agg problem