pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedCoalesce 2 Arrays
7+ messages / 3 participants
[nested] [flat]
* Coalesce 2 Arrays
@ 2019-06-24 20:31 Alex Magnum <magnum11200@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Alex Magnum @ 2019-06-24 20:31 UTC (permalink / raw)
To: pgsql-general
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
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Coalesce 2 Arrays
@ 2019-06-24 21:33 Rob Sargent <robjsargent@gmail.com>
parent: Alex Magnum <magnum11200@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Rob Sargent @ 2019-06-24 21:33 UTC (permalink / raw)
To: Alex Magnum <magnum11200@gmail.com>; +Cc: pgsql-general
> 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?
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Coalesce 2 Arrays
@ 2019-06-24 22:46 Alex Magnum <magnum11200@gmail.com>
parent: Rob Sargent <robjsargent@gmail.com>
0 siblings, 2 replies; 7+ messages in thread
From: Alex Magnum @ 2019-06-24 22:46 UTC (permalink / raw)
To: Rob Sargent <robjsargent@gmail.com>; +Cc: pgsql-general
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?
>
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Coalesce 2 Arrays
@ 2019-06-24 23:11 Rob Sargent <robjsargent@gmail.com>
parent: Alex Magnum <magnum11200@gmail.com>
1 sibling, 1 reply; 7+ messages in thread
From: Rob Sargent @ 2019-06-24 23:11 UTC (permalink / raw)
To: Alex Magnum <magnum11200@gmail.com>; +Cc: pgsql-general
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)
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Coalesce 2 Arrays
@ 2019-06-24 23:12 Rob Sargent <robjsargent@gmail.com>
parent: Alex Magnum <magnum11200@gmail.com>
1 sibling, 0 replies; 7+ messages in thread
From: Rob Sargent @ 2019-06-24 23:12 UTC (permalink / raw)
To: Alex Magnum <magnum11200@gmail.com>; +Cc: pgsql-general
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)
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Coalesce 2 Arrays
@ 2019-06-24 23:19 David G. Johnston <david.g.johnston@gmail.com>
parent: Rob Sargent <robjsargent@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: David G. Johnston @ 2019-06-24 23:19 UTC (permalink / raw)
To: Rob Sargent <robjsargent@gmail.com>; +Cc: Alex Magnum <magnum11200@gmail.com>; pgsql-general
On Mon, Jun 24, 2019 at 4:11 PM Rob Sargent <robjsargent@gmail.com> wrote:
>
> 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)
>
Plain SQL variant:
SELECT array_agg(COALESCE(a, b))
FROM (
SELECT
unnest(ARRAY[null, 2]::int[]),
unnest(ARRAY[1,null]::int[])
) vals (a, b);
Even if they aren't the same length the above should work, I think, as
extra rows for the shorter array will contribute padded nulls.
David J.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Coalesce 2 Arrays
@ 2019-06-24 23:28 Rob Sargent <robjsargent@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Rob Sargent @ 2019-06-24 23:28 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Alex Magnum <magnum11200@gmail.com>; pgsql-general
On 6/24/19 5:19 PM, David G. Johnston wrote:
> On Mon, Jun 24, 2019 at 4:11 PM Rob Sargent <robjsargent@gmail.com
> <mailto:robjsargent@gmail.com>> wrote:
>
>
> 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)
>
> Plain SQL variant:
> SELECT array_agg(COALESCE(a, b))
> FROM (
> SELECT
> unnest(ARRAY[null, 2]::int[]),
> unnest(ARRAY[1,null]::int[])
> ) vals (a, b);
>
> Even if they aren't the same length the above should work, I think, as
> extra rows for the shorter array will contribute padded nulls.
>
> David J.
>
Brilliant of course. Maybe not as easy to stick in another query
select a.name, b.name, tt(a.intarray, b.intarray) as coalesced_array
from table a join table b on a.<something> = b.<something>;
Any guess at the performance differences?
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2019-06-24 23:28 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-06-24 20:31 Coalesce 2 Arrays Alex Magnum <magnum11200@gmail.com>
2019-06-24 21:33 ` Rob Sargent <robjsargent@gmail.com>
2019-06-24 22:46 ` Alex Magnum <magnum11200@gmail.com>
2019-06-24 23:11 ` Rob Sargent <robjsargent@gmail.com>
2019-06-24 23:19 ` David G. Johnston <david.g.johnston@gmail.com>
2019-06-24 23:28 ` Rob Sargent <robjsargent@gmail.com>
2019-06-24 23:12 ` Rob Sargent <robjsargent@gmail.com>
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