title: Array intersection as aggregate function for group by
body: <p>I have the following table:</p>
  
  <pre><code>CREATE TABLE person
  AS
    SELECT name, preferences
    FROM ( VALUES
      ( 'John', ARRAY['pizza', 'meat'] ),
      ( 'John', ARRAY['pizza', 'spaghetti'] ),
      ( 'Bill', ARRAY['lettuce', 'pizza'] ),
      ( 'Bill', ARRAY['tomatoes'] )
    ) AS t(name, preferences);
  </code></pre>
  
  <p>I want to <code>group by person</code> with <code>intersect(preferences)</code> as aggregate function. So I want the following output:</p>
  
  <pre><code>person | preferences
  -------------------------------
  John   | ['pizza']
  Bill   | []
  </code></pre>
  
  <p>How should this be done in SQL? I guess I need to do something like the following, but what does the <code>X</code> function look like?</p>
  
  <pre><code>SELECT    person.name, array_agg(X)
  FROM      person
  LEFT JOIN unnest(preferences) preferences
  ON        true
  GROUP BY  name
  </code></pre>