我有一个包含数值和类别的数据表。我想返回一个新的表格,按类别给出中值,下面是1或2个标准差值,以便进行进一步的分析。我可以使用wiki函数得到中位数,但如下所示,但要与标准差作斗争。

SELECT max(d_voa_record1.unadjustedprice) AS max_sqm_rate
    ,min(d_voa_record1.unadjustedprice) AS min_sqm_rate
    ,count(d_voa_record1.unadjustedprice) AS sample_no
    ,max(d_voa_record1.spec_catcode) AS scat_code
    ,avg(d_voa_record1.unadjustedprice) AS avg_rate
    ,median(unadjustedprice)
FROM processed_data.d_voa_record1
GROUP BY d_voa_record1.spec_catcode;

我用的是Postgres9.6
编辑:As comments解决方案还提供了百分比范围

最佳答案

您可以使用continuous percentile函数找到中值:

percentile_cont(0.5) within group (order by unadjustedprice) as median

然后stddev_samp计算standard deviation
percentile_cont(0.5) within group (order by unadjustedprice) -
     stddev_samp(unadjustedprice) as one_stddev_below_median

你可以乘以2得到低于中值的两个标准差:
percentile_cont(0.5) within group (order by unadjustedprice) -
    2 *  stddev_samp(unadjustedprice) as two_stddev_below_median

关于postgresql - 如何在postgres中返回中值和x标准偏差的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50352013/

10-11 04:59