pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rob Sargent <robjsargent@gmail.com>
To: Alex Magnum <magnum11200@gmail.com>
Cc: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: Coalesce 2 Arrays
Date: Mon, 24 Jun 2019 17:11:46 -0600
Message-ID: <53ea4b16-4a57-e8c9-a27c-5de130375e7c@gmail.com> (raw)
In-Reply-To: <CA+cR4zfGq7wR2TnpVNs2oMDZOcvDnN2xX=8ZmMomTBzeuD28=A@mail.gmail.com>
References: <CA+cR4zeFoa6GS8t6nYPfqFSDc=O3s7Ep5kENOk4c+-dyLKqjFw@mail.gmail.com>
	<163793B6-8981-4618-9770-0AFD4D499728@gmail.com>
	<CA+cR4zfGq7wR2TnpVNs2oMDZOcvDnN2xX=8ZmMomTBzeuD28=A@mail.gmail.com>


On 6/24/19 4:46 PM, Alex Magnum wrote:
> Yes, they are.
>
> On Tue, Jun 25, 2019 at 4:33 AM Rob Sargent <robjsargent@gmail.com 
> <mailto:robjsargent@gmail.com>> wrote:
>
>
>
>>     On Jun 24, 2019, at 2:31 PM, Alex Magnum <magnum11200@gmail.com
>>     <mailto:magnum11200@gmail.com>> wrote:
>>
>>     Hi,
>>     I have two arrays which I need to combine based on the individual
>>     values;
>>     i could do a coalesce for each field but was wondering if there
>>     is an easier way
>>
>>     array_a{a,  null,c,   d,null,f,null}  primary
>>     array_b{null,2  ,null,4,5   ,6,null}  secondary
>>
>>     result {a,  2,   c,   d,5,   f,null)
>>
>>     Any advice would be appreciated
>>
>     Are the inputs always of fixed dimensions eg. 1 by 7?
>
create or replace function tt( a1 int[], a2 int[])
returns int[] as $$
declare
         aret int[];
         asize int;
begin
      select array_length(a1,1) into asize;
      for i in 1..asize loop
           aret[i] = coalesce(a1[i], a2[i]);
      end loop;
      return aret;
end;

$$ language plpgsql;

select * from tt(array[3,null], array[null,4]);
   tt
-------
  {3,4}
(1 row)

view thread (7+ messages)  latest in thread

Message-ID: <53ea4b16-4a57-e8c9-a27c-5de130375e7c@gmail.com>
Permalink:  ../53ea4b16-4a57-e8c9-a27c-5de130375e7c@gmail.com/
Also on:    postgresql.org/message-id/53ea4b16-4a57-e8c9-a27c-5de130375e7c@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, magnum11200@gmail.com
  Subject: Re: Coalesce 2 Arrays
  In-Reply-To: <53ea4b16-4a57-e8c9-a27c-5de130375e7c@gmail.com>
  References: <CA+cR4zeFoa6GS8t6nYPfqFSDc=O3s7Ep5kENOk4c+-dyLKqjFw@mail.gmail.com> <163793B6-8981-4618-9770-0AFD4D499728@gmail.com> <CA+cR4zfGq7wR2TnpVNs2oMDZOcvDnN2xX=8ZmMomTBzeuD28=A@mail.gmail.com> <53ea4b16-4a57-e8c9-a27c-5de130375e7c@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