title: Union of arrays as aggregate function
body: <p>I have the following input:</p>
  
  <pre><code>name  | count | options
  -----------------------
  user1 | 3     | ['option1', 'option2']
  user1 | 12    | ['option2', 'option3']
  user2 | 2     | ['option1', 'option3']
  user2 | 1     | []
  </code></pre>
  
  <p>I want the following output:</p>
  
  <pre><code>name  | count | options
  -----------------------
  user1 | 12    | ['option1', 'option2', 'option3']
  user2 | 2     | ['option1', 'option3']
  </code></pre>
  
  <p>I am grouping by name. For each group, the count should be aggregated as the <code>max</code> and the options should be aggregated as the <code>union</code>. I am having troubles figuring out how do the the latter.</p>
  
  <p>Currently, I have this query:</p>
  
  <pre><code>with data(name, count, options) as (
      select 'user1', 12, array['option1', 'option2']::text[]
      union all
      select 'user1', 12, array['option2', 'option3']::text[]
      union all
      select 'user2', 2, array['option1', 'option3']::text[]
      union all
      select 'user2', 1, array[]::text[]
  )
  select name, max(count)
  from data
  group by name
  </code></pre>
  
  <p><a href="http://rextester.com/YTZ45626" rel="noreferrer">http://rextester.com/YTZ45626</a></p>
  
  <p>I know this can be easily done by defining a custom aggregate function, but I want to do this via a query. I understand the basics of <code>unnest()</code> the array  (and <code>array_agg()</code> the results later on), but cannot figure out how to inject this in my query.</p>