Possible Duplicate:
Intersection of multiple arrays in PostGreSQL
I'm trying to identify the common elements across multiple (>2) arrays in a Pg 9.2 database. The data is structured as:
ID Title Array
1 title1 {a,b,c,d,...}
2 title2 {z,y,a,d,...}
3 title3 {d,a,p,q,...}
n ... ...
From the example above, the SELECT should return {a,d} as the common elements of each array.
I've got this working for 2 arrays as a test case using:
...
select array_agg(e)
from (
select unnest(arr1) from cte_1
intersect
select unnest(arr2) from cte_2
) as dt(e)),
...
But havent been able to get it working correctly with RECURSIVE CTEs. Found some useful examples online and in various Joe Celko books/snippets.
The best I've been able to come up with was this post using custom functions:
Intersection of multiple arrays in PostgreSQL
but it seems there should be a way do accomplish this with a RECURSIVE or other technique.
Any ideas?
Thanks in advance.