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 1hfY8G-0004Vp-K6 for pgsql-general@arkaria.postgresql.org; Mon, 24 Jun 2019 23:12:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hfY8F-0000ql-Fi for pgsql-general@arkaria.postgresql.org; Mon, 24 Jun 2019 23:12:15 +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 1hfY8F-0000qW-1i for pgsql-general@lists.postgresql.org; Mon, 24 Jun 2019 23:12:15 +0000 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hfY8C-0006t4-Kd for pgsql-general@postgresql.org; Mon, 24 Jun 2019 23:12:13 +0000 Received: by mail-pf1-x430.google.com with SMTP id d126so8362730pfd.2 for ; Mon, 24 Jun 2019 16:12:12 -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=SWc7BwxKQOjv3e8KN1cnL03HEuXeABMcR0+7vfXu3Qw=; b=kQaGJRpkHXT4Wo2HY+9FzqvzAuso8Y6zNfQLQ7Tzo0xjVtXlMPjQTbusTeRLBZKmEb 2RgJ3Tnjgv3NPw2K0CongeNOk60EIl3DdoOsz5nunHa9YKcTRdILhGSnPyjfxQOvi7aZ FdLnpR0NhjK+f3A8vcUn2Oc2J6eKTRbGbk3josNpF3gZN0mq1ED9z67mfnu2ZB0aXfll S/W+oRK9MLIQfNH7eXSjL77SP0eYAO1ilIK5GJELcuCndMps+TTsTRVDlV7HNFRrp/Rt bPhADu404g0WtSi3f0yTeFPYsn45R6KmZ6Jut8gM65Ae2LVE4dHvl9aFzhXkca6ux5BH mGSA== 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=SWc7BwxKQOjv3e8KN1cnL03HEuXeABMcR0+7vfXu3Qw=; b=jbd2JO/jyJLAEsAsGArM4fPHqjbnXWrJpFfG503A/oUrLBSgx9oGVx2GcrHyAjCMRy +K90QnmYcS2+Hb3DIgcj0DYAM5mGY4XzcQSs1pG8xAn2O6J7IaP0OcgIoVG6lwdBeYh6 yWJ+/WiHSZYy6/zZ9kbyHuHiytopX4sa60qfavliei+XTfA+CJZFSsn6lfyyDLH9Ax/1 KnhRbwm3upAO/yhxgMaSrq6EgC4aBM2+tFhXG33EBRMU7QQ7skeeoZhpFGBGMN0+vMN0 uiNaHFNJLskct5VI+8whElZfRTdOYkSGqMDMqhaRUw4bAn3UWMrEgtXyMuJumiGsZks5 J1aw== X-Gm-Message-State: APjAAAVBRW0opY9ybTD63cie+6QoicFqsj8/1/ZKGECvmH65hG1bcbEq fc07oLJPyPKHw4/YEs/J005VUh1ZRew= X-Google-Smtp-Source: APXvYqx/aW9Wwy5Tv69F6eKOzGSo5Aw2dGRf5iYsNe7hEdg3WTeTg1yuGKOI7uholQP+rnQ+qDr9JA== X-Received: by 2002:a65:42c4:: with SMTP id l4mr35197496pgp.436.1561417931332; Mon, 24 Jun 2019 16:12:11 -0700 (PDT) Received: from [10.104.134.41] ([155.100.47.1]) by smtp.gmail.com with ESMTPSA id f3sm18610871pfg.165.2019.06.24.16.12.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Jun 2019 16:12:10 -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: <95e3bdde-24e4-e09e-021b-2d83015b243b@gmail.com> Date: Mon, 24 Jun 2019 17:12:10 -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="------------FECE02CE4F1A89087EC8853A" 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. --------------FECE02CE4F1A89087EC8853A 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) --------------FECE02CE4F1A89087EC8853A 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)



--------------FECE02CE4F1A89087EC8853A--