【问题标题】:Finding median value per category查找每个类别的中值
【发布时间】:2020-04-23 23:56:40
【问题描述】:

我有一个数据表,其中包含“字段”和“浓度”列(浓度的缩写)。我正在尝试输出每种类型的字段(类别是 cosmos、egs 等)以及每种字段类型的 conc 统计数据的相关中值。

这是我尝试过的:

SELECT field, percentile_cont(0.5)::numeric FROM galaxies GROUP BY conc LIMIT 5;

错误:函数 percentile_cont(numeric) 不存在

第 1 行:SELECT 字段,percentile_cont(0.5)::numeric FROM galaxies GR...

但是,我收到了这个错误,我不确定如何使用每个字段类型的 conc 中值提取字段名称

【问题讨论】:

    标签: postgresql median


    【解决方案1】:

    如 PostgreSQL 的 the documentation 中所述,WITHIN GROUP (ORDER BY...) 对于像 percentile_cont 这样的有序集聚合函数是必需的。

    如果您想要 conc 的中位数,那么按 conc 分组肯定是错误的做法。所以你可能想要这样的东西:

    SELECT field, percentile_cont(0.5) within group (order by conc)::numeric 
        FROM galaxies GROUP BY field LIMIT 5;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      • 2020-08-21
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多