pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rob Sargent <robjsargent@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Converting row elements into a arrays?
Date: Thu, 2 Mar 2023 15:10:24 -0700
Message-ID: <660edb74-9a49-8d68-c489-7de99d955c60@gmail.com> (raw)
In-Reply-To: <36f53642-db43-4fc9-028e-8b6cd3dff9b4@gmail.com>
References: <788ab733-d0d1-7791-fb22-7c2724b08816@gmail.com>
	<e994b049-93f1-d739-e74c-ecd29bbbe4c5@gmail.com>
	<36f53642-db43-4fc9-028e-8b6cd3dff9b4@gmail.com>

On 3/2/23 14:49, Ron wrote:
> On 3/2/23 15:45, Rob Sargent wrote:
>> On 3/2/23 13: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}
>>>
>>> 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);
>>>
> [snip]
>>
>> In which environment are you accessing that array?  psql only?
>
> Correct.
>

For multiple columns it's a litte messier ( if you have different data 
types in your columns)

create table foo (f1 int, f2 int, f3 text);
insert into foo values (1, 1,'asdf');
insert into foo values (1, 2, 'qerr');

select f1, array_agg(array[f2::text,f3]) from foo group by f1;
  f1 |      array_agg
----+---------------------
   1 | {{1,asdf},{2,qerr}}
(1 row)

view thread (11+ messages)  latest in thread

Message-ID: <660edb74-9a49-8d68-c489-7de99d955c60@gmail.com>
Permalink:  ../660edb74-9a49-8d68-c489-7de99d955c60@gmail.com/
Also on:    postgresql.org/message-id/660edb74-9a49-8d68-c489-7de99d955c60@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: robjsargent@gmail.com
  Subject: Re: Converting row elements into a arrays?
  In-Reply-To: <660edb74-9a49-8d68-c489-7de99d955c60@gmail.com>
  References: <788ab733-d0d1-7791-fb22-7c2724b08816@gmail.com> <e994b049-93f1-d739-e74c-ecd29bbbe4c5@gmail.com> <36f53642-db43-4fc9-028e-8b6cd3dff9b4@gmail.com> <660edb74-9a49-8d68-c489-7de99d955c60@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox