Nested aggregate functions with grouping in postgresql
You need a subquery:
select z, avg(sumval)from (select y, z, sum(x) as sumval from t group by y, z ) tgroup by z
You could also use a Common Table Expression (CTE) for that:
WITH tmp AS ( SELECT y, z, sum(x) as sumval FROM t GROUP BY y, z)SELECT z, avg(sumval)FROM tmpGROUP BY z