嵌套聚合函数,在postgresql中进行分组

时间:2022-06-01 17:51:24

I'm trying to get an average of sums using nested aggregate functions and grouping. What I would want to do is:

我试图使用嵌套聚合函数和分组来获得平均值。我想要做的是:

SELECT AVG(SUM(x) GROUP BY y) WHERE ... GROUP BY ...;

That is, for each row returned, I want one of the fields to be an average of sums, where each sum is over the rows where y is the same.

也就是说,对于返回的每一行,我希望其中一个字段是和的平均值,其中每个和是在y相同的行上。

I would like to avoid subselects if possible.

如果可能的话,我想避免使用子选择。

1 个解决方案

#1


14  

You need a subquery:

你需要一个子查询:

select z, avg(sumval)
from (select y, z, sum(x) as sumval
      from t
      group by y, z
     ) t
group by z

#1


14  

You need a subquery:

你需要一个子查询:

select z, avg(sumval)
from (select y, z, sum(x) as sumval
      from t
      group by y, z
     ) t
group by z