sql - 计算postgres中数据频率的函数

标签 sql postgresql function greenplum

我想按月计算数字变量的均值、标准差、百分位数 (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

最佳答案

思路是:

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 - 计算postgres中数据频率的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50419997/

相关文章:

java - java和数据库之间共享常量

java - Hibernate SQL findin 最常出现的数字是多少?

sql - 更新股票信息的 PostgreSQL 合并查询 - "MERGE"处或附近的语法错误

python - writer.writerow 不适用于在 for 循环中写入多个 CSV

mysql - 找到上周客户的有效方法

sql - 使用现有命名空间查询 Oracle 中的 XML 表

sql - 仅当列为 NULL 时加入

sql-server - Postgresql 触发器通过 ODBC 将数据写入 SQL Server?

javascript - "Unexpected end of input"- 代码学院剪刀石头布

c++ - 我可以从以前的参数中设置默认参数吗?