using Postgres 9.3...
Can someone please explain why I can't use a max function directly on an unnested array..?
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?)
table: foo_history:
id | history::smallint
-----------------------------------
1 | {10,20,30,50,40}
This doesn't work ?
Select id, max(unnest(history)) as vMax from foo_history;
...but this one does...?
WITH foo as (
select id, unnest(history) as history
from foo_history
)
Select
id, max(history) as vMax
From foo
Group by id;
max
is an aggregate, so it operates on one input per tuple. It can't take a set as an input like that. To do what you describe the way you express it you'd use a subquery, likeselect id, (select max(x) from unnest(history) x) as vmax from foo_history
. – Farmyard