Re: Converting row elements into a arrays? - Mailing list pgsql-general

From Ray O'Donnell
Subject Re: Converting row elements into a arrays?
Date
Msg-id 18b226e4-d018-c3b1-9dae-378bf67af346@rodonnell.ie
Whole thread Raw
In response to Converting row elements into a arrays?  (Ron <ronljohnsonjr@gmail.com>)
Responses Re: Converting row elements into a arrays?  (Ray O'Donnell <ray@rodonnell.ie>)
Re: Converting row elements into a arrays?  (Ron <ronljohnsonjr@gmail.com>)
List pgsql-general
On 02/03/2023 20:58, Ron wrote:
> Postgresql 12.13
> 
> Given the sample below, I'm looking for how to generate this output.  
> It's like GROUP BY, but generating an array instead of an aggreate number.
>   f1 | f2_array
> ----+---------
> 1 | {1,2,3}
>    2 | {1,2,3,4}
>    3 | {1,2}

Something like this (off the top of my head)? -

    select f1, array_agg(f2) as f2_array group by f1;

Hope that helps (and that it's right!).

Ray.



> 
> The ultimate goal is to somehow use pg_index.indkey to get column names 
> from pg_attribute.
> 
> create table foo (f1 int, f2 int);
> insert into foo values (1, 1);
> insert into foo values (1, 2);
> insert into foo values (1, 3);
> insert into foo values (2, 1);
> insert into foo values (2, 2);
> insert into foo values (2, 3);
> insert into foo values (2, 4);
> insert into foo values (3, 1);
> insert into foo values (3, 2);
> 
> select * from foo order by f1, f2;
>   f1 | f2
> ----+----
>    1 |  1
>    1 |  2
>    1 |  3
>    2 |  1
>    2 |  2
>    2 |  3
>    2 |  4
>    3 |  1
>    3 |  2
> (9 rows)
> 
> 
> 

-- 
Raymond O'Donnell // Galway // Ireland
ray@rodonnell.ie




pgsql-general by date:

Previous
From: Ron
Date:
Subject: Converting row elements into a arrays?
Next
From: Ray O'Donnell
Date:
Subject: Re: Converting row elements into a arrays?