postgresql 使用基本数学函数查找标准差

tf7tbtn2  于 12个月前  发布在  PostgreSQL
关注(0)|答案(1)|浏览(311)

我试图从一个包含收入值的表中获得标准差,使用postgresql中的基本数学函数。
这就是我所尝试的:

SELECT sqrt(sum(power(income - (sum(income) / count(income)), 2)) / (count(*) - 1)) FROM income_data

然而,我总是得到以下错误:

ERROR: aggregate function calls cannot be nested

有人遇到过这个问题吗?我觉得获得标准差的逻辑应该是可行的,虽然到目前为止还没有任何运气,我感谢任何关于如何解决的建议。

1l5u6lss

1l5u6lss1#

您应该在单独的查询中计算平均值,例如在with语句中:

with mean as (
    select sum(income) / count(income) as mean
    from income_data
)
select sqrt(sum(power(income - mean, 2)) / (count(*) - 1)) 
from income_data
cross join mean;

或在派生表中:

select sqrt(sum(power(income - mean, 2)) / (count(*) - 1)) 
from income_data
cross join (
    select sum(income) / count(income) as mean
    from income_data
) s;

相关问题