SQL - 如何计算x / y的标准差(stddev)?

时间:2016-03-07 21:33:00

标签: mysql sql

示例:计算点击率(CTR)

CAST(SUM(num_clicks) as DOUBLE) / CAST(SUM(num_impression) as DOUBLE) as CTR

如何计算CTR的标准偏差? stddev(CTR)不会工作,因为它将是一个嵌套的混合函数...而且,我不能先计算CTR然后再运行stddev。

1 个答案:

答案 0 :(得分:1)

您可以先计算点击率,然后使用子查询在其上运​​行STDDEV_POP

SELECT STDDEV_POP(ctr) AS ctr_stddev
FROM (
    SELECT CAST(SUM(num_clicks) as DOUBLE) / CAST(SUM(num_impression) as DOUBLE) AS ctr
    FROM yourTable
    WHERE ...
    GROUP BY ad_id) AS temp