【发布时间】:2019-06-19 01:29:07
【问题描述】:
我想退货。 该函数获取一个数组(查询是'select function_name(array_agg(column_name)) from table_name')
我在下面编码:
create type pddesctype as(
count float,
mean float,
std float,
min float
);
create function pddesc(x numeric[])
returns pddesctype
as $$
import pandas as pd
data=pd.Series(x)
count=data.describe()[0]
mean=data.describe()[1]
std=data.describe()[2]
min=data.describe()[3]
return count, mean, std, min
$$ language plpython3u;
此代码仅导致一列上的数组。 (浮动,浮动,浮动...)
我试过了
create function pddesc(x numeric[])
returns table(count float, mean float, std float, min float)
as $$
import pandas as pd
data=pd.Series(x)
count=data.describe()[0]
mean=data.describe()[1]
std=data.describe()[2]
min=data.describe()[3]
return count, mean, std, min
$$ language plpython3u;
但是有一个错误:
ERROR: key "count" not found in mapping
HINT: To return null in a column, add the value None to the mapping with the key named after the column.
CONTEXT: while creating return value.
我想在不预先创建类型的情况下以列(如表格)显示结果。
如何更改 RETURN / RETURNS 语法?
【问题讨论】:
-
如果你想返回一个表,那么使用
returns table (...) -
我应该创建一个返回值更新的表吗?
-
返回表格的函数必须像表格一样使用
select * from pddesc(...) -
我需要从另一个表中获取数据,所以我尝试了
select * from (select pddesc(array_agg(column_name)) from table_name) as result。但是,我在一列上得到数组数据。你知道另一种获取餐桌的方法吗? -
我处理这个问题。我在 python 代码中使用了 plpy 查询。谢谢
标签: postgresql plpython postgres-plpython