You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Presto中基于avg_total_orders_last_12_months计算30天平均订单标准差

错误点说明

  1. 语法错误:customer_id字段后缺少逗号,直接触发执行报错
  2. 函数使用错误:approx_distinct是近似去重计数函数、SUM是求和函数,均和标准差计算无关,Presto内置了专门的标准差函数:
    • stddev(字段):计算样本标准差
    • stddev_pop(字段):计算总体标准差
  3. 分区逻辑错误:按customer_id分区时,每个分区仅对应1条用户数据,计算出的标准差永远为0,无统计意义,需按你指定的avg_total_orders_last_12_months字段分组/分区统计。

修正后SQL(按avg_total_orders_last_12_months分组计算对应组内的近30天订单均值标准差)

select 
    avg_total_orders_last_12_months,
    stddev_pop(avg_total_orders_last_30_days) as stdev_rep
from table
group by 1

如果你需要保留所有原始行,每行附带对应同近12月订单均值分组的标准差,使用窗口函数写法

select 
    customer_id,
    avg_total_orders_last_30_days,
    avg_total_orders_last_12_months,
    stddev_pop(avg_total_orders_last_30_days) OVER (partition by avg_total_orders_last_12_months) as stdev_rep
from table

内容的提问来源于stack exchange,提问作者user12625679

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 18:15:04