用于计算postgress

时间:2018-05-18 22:27:41

标签: sql postgresql function greenplum

我想计算数值变量的平均值,标准差,百分位数(25,50,75),分类变量和频率的频率。按月分类和数字变量的NULL频率.Below只是示例数据。我有20 +列和15k +记录。我想要执行函数。

    Date          id  score_n  score_p  score_s  Reason 

 31-12-2016       1   0.5       6      5.0      energy_drink
 31-12-2016       4     6       3       3       soft_drink
 31-12-2016       5     3       4       2       energy_drink

1 个答案:

答案 0 :(得分:2)

这个想法是:

select date_trunc('month', date) as yyyymm,
       avg(score_n) as avg, stddev(score_n),
       percentile_cont(0.25) within group (order by score_n),
       percentile_cont(0.50) within group (order by score_n),
       percentile_cont(0.75) within group (order by score_n)
from t
group by date_trunc('month', date);

您可以查看documentation中的汇总功能。