Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hfY7u-0004Ui-6d for pgsql-general@arkaria.postgresql.org; Mon, 24 Jun 2019 23:11:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hfY7s-0006Mb-AM for pgsql-general@arkaria.postgresql.org; Mon, 24 Jun 2019 23:11:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hfY7r-0006MI-Qg for pgsql-general@lists.postgresql.org; Mon, 24 Jun 2019 23:11:52 +0000 Received: from mail-pl1-x642.google.com ([2607:f8b0:4864:20::642]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hfY7p-0006sM-4T for pgsql-general@postgresql.org; Mon, 24 Jun 2019 23:11:50 +0000 Received: by mail-pl1-x642.google.com with SMTP id b7so7739693pls.6 for ; Mon, 24 Jun 2019 16:11:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=eHOm2mvnHkBrkVu81EP50vOk+vWMEKApEMJRA8bvmAY=; b=aZaMJcqsBAPPz1f5pGk+gCg3j2xMzclph9jHSz7EVrTky7nYKpSlxch8/PKm2HifnZ KHq3Ioorb7T/OJ2MJPLj0PF6bZo6wutBrxYYf8SymnMnQ8JO8Qz6GagF4CB+C/Fi9c3z txdmxRG+O8FUmaX2W1Vf3hNxrN2QFasLG7P11J6k6mdaxFcJlfb2t3sA15bv0Od53riK Yzh65/zTwIAbotv6x6xPHaSt4mfkTV2lFPmW/Q3oyrZPkOE3ucscvUK5p0mqstq5ItHj AKmltKW+5BmltEiy/t0nLkFNJXJ6n6aGILPSk4W5bgsl8NqpbvwD6tOfM8EsG+PRFmBs M0Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=eHOm2mvnHkBrkVu81EP50vOk+vWMEKApEMJRA8bvmAY=; b=d260CeRTKE2tGG09taIcRP3Atm1m/BjI12/SNHX/BN5rRkXRgZIvmY+RWG24Ex+KLE kIMZFZAy1SC0Ex2zpkDH+luGZh4dkxGafMcNEJL340+zjHDUJre6qJYGp6Str2abHDXA 1McpX8sLWM4tnMHsLSv7sv82GxIP+WfH+WJcGbz+KsA9m+RQKtAsvvWevAQ7zIrKWCU0 H7WRICCexIuezbLG9Pqx9alY6IKyrYXab7E/hdaaoIEByNChSAAKYV6YDoUc7j+ZlJPZ 7nD2lGY88aP8RyEYmn9H+HlX3nFyZtLlX8rVX4yVKSWLo4IN+y+9e4u6DN4lN55OdLd8 OfkA== X-Gm-Message-State: APjAAAXt6+xX6Gm/vczCvKOjnFbsr8AfOELKNMV94t/+I43SfYvEhBBx 4j5afyiqj87pTYxOyn0cY1cyNich6lE= X-Google-Smtp-Source: APXvYqz40uZEJof1e9wwaCM4hUVsKtwywU1uY7t5Nt6MbNUlLAKqSfvNBdECC+SIIRc/MUYIQLRjdA== X-Received: by 2002:a17:902:968c:: with SMTP id n12mr58207663plp.59.1561417907955; Mon, 24 Jun 2019 16:11:47 -0700 (PDT) Received: from [10.104.134.41] ([155.100.47.1]) by smtp.gmail.com with ESMTPSA id t96sm520490pjb.1.2019.06.24.16.11.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Jun 2019 16:11:47 -0700 (PDT) Subject: Re: Coalesce 2 Arrays To: Alex Magnum Cc: "pgsql-general@postgresql.org" References: <163793B6-8981-4618-9770-0AFD4D499728@gmail.com> From: Rob Sargent Message-ID: <53ea4b16-4a57-e8c9-a27c-5de130375e7c@gmail.com> Date: Mon, 24 Jun 2019 17:11:46 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------CC323C460FE2F83EB27130BB" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------CC323C460FE2F83EB27130BB Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 6/24/19 4:46 PM, Alex Magnum wrote: > Yes, they are. > > On Tue, Jun 25, 2019 at 4:33 AM Rob Sargent > wrote: > > > >> On Jun 24, 2019, at 2:31 PM, Alex Magnum > > 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) --------------CC323C460FE2F83EB27130BB Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit


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> wrote:


On Jun 24, 2019, at 2:31 PM, Alex Magnum <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)


--------------CC323C460FE2F83EB27130BB--