尝试创建聚合函数:
create aggregate min (my_type) ( sfunc = least,stype = my_type ); ERROR: syntax error at or near "least" LINE 2: sfunc = least,^
我错过了什么?
Although the manual calls least
a function:
The GREATEST and LEAST functions select the largest or smallest value from a list of any number of expressions.
我找不到它了:
\dfS least List of functions Schema | Name | Result data type | Argument data types | Type --------+------+------------------+---------------------+------ (0 rows)
解决方法
至少和最伟大的不是真正的功能;在内部,它们被解析为MinMaxExpr(参见src / include / nodes / primnodes.h).
你可以使用这样的通用函数实现你想要的:
CREATE FUNCTION my_least(anyelement,anyelement) RETURNS anyelement LANGUAGE sql IMMUTABLE CALLED ON NULL INPUT AS 'SELECT LEAST($1,$2)';
(感谢Erwin Brandstetter关于CALLED ON NULL INPUT以及使用LEAST的想法.)
然后,您可以创建聚合
CREATE AGGREGATE min(my_type) (sfunc = my_least,stype = my_type);
这只适用于my_type的比较函数,否则你必须提出不同的my_least函数.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。