Presto中基于avg_total_orders_last_12_months计算30天平均订单标准差
错误点说明
- 语法错误:
customer_id字段后缺少逗号,直接触发执行报错 - 函数使用错误:
approx_distinct是近似去重计数函数、SUM是求和函数,均和标准差计算无关,Presto内置了专门的标准差函数:stddev(字段):计算样本标准差stddev_pop(字段):计算总体标准差
- 分区逻辑错误:按
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
相关产品推荐
相关产品推荐

