【问题标题】:Function to calculate frequencies of the data in postgress计算postgres中数据频率的函数
【发布时间】:2018-05-18 22:27:41
【问题描述】:

我想按月计算数值变量的均值、标准差、百分位数 (25,50,75)、分类变量的频率和分类变量和数值变量的 NULL 频率。下面是示例数据。我有像 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

【问题讨论】:

标签: sql postgresql function greenplum


【解决方案1】:

想法是:

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中的聚合函数。

【讨论】:

  • ,谢谢。 sql逻辑?
  • 您需要使用cron 或您环境中的其他调度程序来安排作业。
  • 我们可以把它写成存储函数/过程吗?
猜你喜欢
  • 1970-01-01
  • 2021-12-12
  • 2021-10-07
  • 1970-01-01
  • 2010-11-28
  • 1970-01-01
  • 2017-10-25
  • 1970-01-01
相关资源
最近更新 更多