【发布时间】:2014-04-02 04:44:42
【问题描述】:
我想在 pgsql 中找到一些数据的中值。一个快速的谷歌搜索告诉我 PGSQL 8.2 没有中值函数。经过更多搜索后,我找到了此链接 https://wiki.postgresql.org/wiki/Aggregate_Median
它提供了一些关于如何编写自定义中值函数的信息。这是我到目前为止的代码
CREATE OR REPLACE FUNCTION my_schema.final_median(anyarray) RETURNS float8 STRICT AS
$$
DECLARE
cnt INTEGER;
BEGIN
cnt := (SELECT count(*) FROM unnest($1) val WHERE val IS NOT NULL);
RETURN (SELECT avg(tmp.val)::float8
FROM (SELECT val FROM unnest($1) val
WHERE val IS NOT NULL
ORDER BY 1
LIMIT 2 - MOD(cnt, 2)
OFFSET CEIL(cnt/ 2.0) - 1
) AS tmp
);
END
$$ LANGUAGE plpgsql;
CREATE AGGREGATE my_schema.mymedian(anyelement) (
SFUNC=array_append,
STYPE=anyarray,
FINALFUNC=my_schema.final_median,
INITCOND='{}'
);
-- I need this filter here. This is a place holder for a larger query
select my_schema.mymedian(id) filter (where id < 5)
from my_schema.golf_data
但是当我运行代码时出现错误
ERROR: function my_schema.mymedian(numeric) is not defined as STRICT
LINE 27: select my_schema.mymedian(id) filter (where id < 5)
^
HINT: The filter clause is only supported over functions defined as STRICT.
********** Error **********
ERROR: function my_schema.mymedian(numeric) is not defined as STRICT
SQL state: 0AM00
Hint: The filter clause is only supported over functions defined as STRICT.
Character: 661
我猜解释器希望我在某处添加关键字 strict。但我不确定我需要在哪里进行此更改。
任何帮助将不胜感激
【问题讨论】:
-
有趣的是,
FILTER子句将在 9.4 中可用:postgresql.org/docs/devel/static/… - 你确定,你使用的是 8.2:postgresql.org/docs/8.2/static/… 吗? -
我运行了 select version(),这里是 x86_64-unknown-linux-gnu 上的输出“PostgreSQL 8.2.15 (Greenplum Database 4.2.7.1 build 1),由 GCC gcc (GCC) 编译4.4.2编译于2014年2月13日19:33:14"
-
这似乎根本不是 postgresql:Greenplum 数据库建立在开源数据库 PostgreSQL 的基础之上。 en.wikipedia.org/wiki/Greenplum#cite_ref-11
标签: postgresql syntax-error median