2

I am trying to write a query to match more than one element in postgres arrays

Array1 Array2 More than one element Match
a,b,c,d a FALSE
a,b,c,d a,b TRUE
a,b,c,d a,b,c TRUE
a,b,c,d a,d TRUE
a,b,c,d a,c,e TRUE
a,b,c,d a,b,c,d TRUE
a,b,c,d a,b,e,d,f TRUE
a,b,c,d a,z,e,f,g,h FALSE

I tried with all of these ops https://www.postgresql.org/docs/current/functions-array.html

Is there any inbuilt way to do it?

3
  • What if you have d,c,b,a and a,b,c,d? (order of elements in an array matter) Commented Nov 10, 2021 at 8:03
  • 1
    Why is the result on line 4 FALSE? Both a and d match. Commented Nov 10, 2021 at 8:45
  • @Stefanov.sm Thank you I have corrected it Commented Nov 10, 2021 at 14:11

2 Answers 2

2

Unnest and join the arrays in a scalar subquery; Count the number of resulting rows and check if it is greater than 1.

select array1, array2, 
  (
    select count(*) 
    from unnest(array1) t1 
    join unnest(array2) t2 on t1 = t2
  ) > 1
from the_table;

This is if array elements are unique and the order of elements in the arrays does not matter.

Update
To make it work correctly with repeating array elements replace unnest(arrayx) tx with (select distinct t from unnest(arrayx) t) tx

select array1, array2, 
(
 select count(*) 
 from (select distinct t from unnest(array1) t) t1 
 join (select distinct t from unnest(array2) t) t2 on t1 = t2
) > 1 from the_table;
Sign up to request clarification or add additional context in comments.

Comments

1

Write a subquery and inside of it unnest both arrays, and then make a cross join with all elements, e.g.

SELECT   
  array1, array2,
  (SELECT count(*) FILTER (WHERE el1 = el2)
   FROM unnest(t1.array1) a1(el1), unnest(t1.array2)a2(el2)) > 1
FROM t t1;

EDIT (See comments below): In case the array elements do repeat (opposite the given data sample) and you wish to count the overlap only once, just unnest the arrays with another subquery using DISTINCT, e.g.

SELECT   
  array1, array2,
  (SELECT count(*) FILTER (WHERE el1 = el2)
   FROM (SELECT DISTINCT unnest(t1.array1)) a1(el1), 
        (SELECT DISTINCT unnest(t1.array2)) a2(el2)) > 1
FROM t t1;

Demo: db<>fiddle

3 Comments

This query produces wrong results if the array elements are not unique.
@Stefanov.sm I also thought about it and in fact you're right. I just left this option out because the data sample suggested a different scenario: I edited my question to make your point. thanks for pointing out. cheers and happy coding
Cheers to you too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.