PL / R - 将整列作为参数传递

时间:2015-02-19 09:21:06

标签: r greenplum plr

我正在尝试编写一个简单的PL / R函数来查找列的平均值(Greenplum 4.3.4 DB)

CREATE OR REPLACE FUNCTION mean_plr(val numeric[]) RETURNS numeric AS
$$
x <- val
avg = mean(x)
return(avg)
$$
LANGUAGE 'plr';

SELECT mean_plr(sensor1) FROM public.tempSensors;

然而,这给了我错误:

ERROR:  function mean_plr(numeric) does not exist
LINE 1: SELECT mean_plr(sensor1) FROM public.tempSensors;
               ^
HINT:  No function matches the given name and argument types. You may need to add explicit type casts.

1 个答案:

答案 0 :(得分:1)

弄清楚我做错了什么。 mean_plr(sensor1)一次发送1个值。要发送整个列,我们需要使用array_agg()函数。

SELECT mean_plr(array_agg(sensor1::numeric)) FROM public.tempSensors;