Ask Your Question
0

Determine how many elements in a Postgres array overlap with each other.

asked 2023-05-29 18:33:47 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-29 18:36:01 +0000

lakamha gravatar image

To determine how many elements in a Postgres array overlap with each other, you can use the OVERLAPS operator along with the array intersection operator &&.

Here's an example query that returns the count of overlapping elements in two arrays:

SELECT COUNT(*) FROM unnest(ARRAY[1, 2, 3, 4]) a
WHERE a = ANY(ARRAY[3, 4, 5, 6]) AND a && ARRAY[3, 4, 5, 6];

This will return a count of 2, indicating that the arrays overlap on elements 3 and 4.

You can adjust the arrays in the query to compare any two arrays and determine the count of overlapping elements.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-29 18:33:47 +0000

Seen: 10 times

Last updated: May 29 '23