title: Postgres: Finding max value in an int array?
body: <p><strong>using Postgres 9.3...</strong></p>
  
  <p>Can someone please explain why I can't use a max function directly on an unnested array..?</p>
  
  <p>It is my understanding the unnest function returns a "setof" just like the select statement.  So why does the short version of this query not work?  (Am I missing something conceptually or is my issue a syntax related one?)</p>
  
  <pre><code>table: foo_history: 
  
  id | history::smallint
  ----------------------------------- 
  1  |  {10,20,30,50,40}
  </code></pre>
  
  <p>This doesn't work ?</p>
  
  <pre><code>Select id, max(unnest(history)) as vMax from foo_history;
  </code></pre>
  
  <p>...but this one does...?</p>
  
  <pre><code>WITH foo as (
      select id, unnest(history) as history 
      from foo_history
  )
  Select 
      id, max(history) as vMax
  From foo 
  Group by id;
  </code></pre>