with myTable as (
select array_construct('a', 'b', 'c', 'd', 'e') as a1
,array_construct('a', 'f', 'c', 'g', 'e') as a2
)
select a1, a2, array_intersection(a1, a2)
from myTable;
показывает, что мы работаем с одними и теми же данными.
with myTable as (
SELECT array_construct('a', 'b', 'c', 'd', 'e') as a1
,array_construct('a', 'f', 'c', 'g', 'e') as a2
), seq_myTable as (
SELECT seq8() as seq
,t.*
from myTable t
), expanded_a1 as (
select a.seq
,f.value as val
from seq_myTable a,
lateral flatten(input => a.a1) f
), expanded_a2 as (
select a.seq
,f.value as val
from seq_myTable a,
lateral flatten(input => a.a2) f
)
select coalesce(a.seq,b.seq) as seq, array_agg(coalesce(a.val,b.val)) as vals
from expanded_a1 a
full outer join expanded_a2 b
on a.seq = b.seq and a.val = b.val
where (a.seq is null OR b.seq is null)
group by 1;
это дает ответы, но они не отсортированы, для чего вам нужно:
with myTable as (
SELECT array_construct('a', 'b', 'c', 'd', 'e') as a1
,array_construct('a', 'f', 'c', 'g', 'e') as a2
), seq_myTable as (
SELECT seq8() as seq
,t.*
from myTable t
), expanded_a1 as (
select a.seq
,f.value as val
from seq_myTable a,
lateral flatten(input => a.a1) f
), expanded_a2 as (
select a.seq
,f.value as val
from seq_myTable a,
lateral flatten(input => a.a2) f
)
select array_agg(val) WITHIN GROUP ( order by val) as vals
from (
select coalesce(a.seq,b.seq) as seq, coalesce(a.val,b.val) as val
from expanded_a1 a
full outer join expanded_a2 b
on a.seq = b.seq and a.val = b.val
where (a.seq is null OR b.seq is null)
)
group by seq;
дает вывод [ "b", "d", "f", "g" ]