【问题标题】:Finding standard deviation using basic math functions使用基本数学函数查找标准差
【发布时间】:2017-12-14 19:17:05
【问题描述】:

我正在尝试使用 postgresql 中的以下基本数学函数从包含收入值的表中获取标准差。

这是我尝试过的:

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

但是,我不断收到以下错误:

ERROR: aggregate function calls cannot be nested

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

【问题讨论】:

  • 只需使用stddev()。无需重新发明轮子。
  • 是的,我知道 stddev 使这更容易,但是,我们的想法是使用上面列出的基本数学函数并完成与使用 stddev 时相同的结果

标签: sql postgresql


【解决方案1】:

您应该在单独的查询中计算平均值,例如在 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多