将额外的参数传递给PostgreSQL聚合最终函数 [英] Pass extra parameter to PostgreSQL aggregate final function
问题描述
将额外的参数传递给PostgreSQL集合的最终函数以为状态值创建特殊的TYPE的唯一方法是吗?
Is the only way to pass an extra parameter to the final function of a PostgreSQL aggregate to create a special TYPE for the state value?
例如:
CREATE TYPE geomvaltext AS (
geom public.geometry,
val double precision,
txt text
);
然后使用此类型作为状态变量,以便第三个参数(文本)最终到达
And then to use this type as the state variable so that the third parameter (text) finally reaches the final function?
为什么聚合自身不能将额外的参数传递给最终函数?实施的原因是什么?
Why aggregates can't pass extra parameters to the final function themselves? Any implementation reason?
因此,我们可以轻松构造例如采用以下方法的聚合:
So we could easily construct, for example, aggregates taking a method:
SELECT ST_MyAgg(accum_number, 'COMPUTE_METHOD') FROM blablabla
SELECT ST_MyAgg(accum_number,'COMPUTE_METHOD') p>
Thanks
推荐答案
您可以定义具有多个参数的聚合。
You can define an aggregate with more than one parameter.
I不知道这是否可以解决您的问题,但是您可以像这样使用它:
I don't know if that solves your problem, but you could use it like this:
CREATE OR REPLACE FUNCTION myaggsfunc(integer, integer, text) RETURNS integer
IMMUTABLE STRICT LANGUAGE sql AS
$f$
SELECT CASE $3
WHEN '+' THEN $1 + $2
WHEN '*' THEN $1 * $2
ELSE NULL
END
$f$;
CREATE AGGREGATE myagg(integer, text) (
SFUNC = myaggsfunc(integer, integer, text),
STYPE = integer
);
可以这样使用:
CREATE TABLE mytab
AS SELECT * FROM generate_series(1, 10) i;
SELECT myagg(i, '+') FROM mytab;
myagg
-------
55
(1 row)
SELECT myagg(i, '*') FROM mytab;
myagg
---------
3628800
(1 row)
这篇关于将额外的参数传递给PostgreSQL聚合最终函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!