postgresql整数数组的总和和长度?
sum and length of postgresql integer array?
我正在尝试对 postgresql 数组的内容求和并确定其长度。在所有情况下,我都使用整数数组类型的列。
ERROR: function sum(integer[]) does not exist
LINE 1: select sum(interested) from deals;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
db5=> select SUM(interested) from deals;
ERROR: function sum(integer[]) does not exist
LINE 1: select SUM(interested) from deals;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
db5=> select array_length(interested) from deals;
ERROR: function array_length(integer[]) does not exist
LINE 1: select array_length(interested) from deals;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
db5=>
与我读到的相反 (Select sum of an array column in PostgreSQL, http://www.postgresql.org/docs/8.4/static/functions-array.html) sum(column) 和 array_length(column, 1) 似乎没有按我的预期执行。我在看错误的文档吗?如何获得 postgre 整数数组的总和和长度?
此外,这些是我正在使用的 php 查询以及 pg_query 到 运行 这些调用:
$query = "UPDATE deals set remaining = max - array_length(interested, 1), until = min -array_length(interested, 1";
$query = "UPDATE deals set remaining = max - (SELECT SUM FROM UNNEST(hours)), until = min - (SELECT SUM FROM UNNEST(hours))";
我修改了它们以考虑 Patrick 从评论中提出的建议,但我仍然遇到语法错误,例如
Warning: pg_query(): Query failed: ERROR: syntax error at end of input LINE 1: ...ngth(interested, 1), until = min -array_length(interested, 1 ^ in /var/www/html/join.php on line 162
谢谢。
在更新语句中,您只是忘记了语句末尾的括号(见评论)。
第二条update语句,不能在UPDATE中使用sum,因为update中不允许使用聚合函数。
使用自定义 sql 函数对数组中的所有元素求和并在更新中使用它:
CREATE FUNCTION array_sum(NUMERIC[]) returns numeric AS
$$
SELECT sum(unnest) FROM (select unnest()) as foo;
$$
LANGUAGE sql;
更新语句:
UPDATE deals SET remaining = max - array_sum(hours), until = min - array_sum(hours);
我正在尝试对 postgresql 数组的内容求和并确定其长度。在所有情况下,我都使用整数数组类型的列。
ERROR: function sum(integer[]) does not exist
LINE 1: select sum(interested) from deals;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
db5=> select SUM(interested) from deals;
ERROR: function sum(integer[]) does not exist
LINE 1: select SUM(interested) from deals;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
db5=> select array_length(interested) from deals;
ERROR: function array_length(integer[]) does not exist
LINE 1: select array_length(interested) from deals;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
db5=>
与我读到的相反 (Select sum of an array column in PostgreSQL, http://www.postgresql.org/docs/8.4/static/functions-array.html) sum(column) 和 array_length(column, 1) 似乎没有按我的预期执行。我在看错误的文档吗?如何获得 postgre 整数数组的总和和长度?
此外,这些是我正在使用的 php 查询以及 pg_query 到 运行 这些调用:
$query = "UPDATE deals set remaining = max - array_length(interested, 1), until = min -array_length(interested, 1";
$query = "UPDATE deals set remaining = max - (SELECT SUM FROM UNNEST(hours)), until = min - (SELECT SUM FROM UNNEST(hours))";
我修改了它们以考虑 Patrick 从评论中提出的建议,但我仍然遇到语法错误,例如
Warning: pg_query(): Query failed: ERROR: syntax error at end of input LINE 1: ...ngth(interested, 1), until = min -array_length(interested, 1 ^ in /var/www/html/join.php on line 162
谢谢。
在更新语句中,您只是忘记了语句末尾的括号(见评论)。
第二条update语句,不能在UPDATE中使用sum,因为update中不允许使用聚合函数。 使用自定义 sql 函数对数组中的所有元素求和并在更新中使用它:
CREATE FUNCTION array_sum(NUMERIC[]) returns numeric AS
$$
SELECT sum(unnest) FROM (select unnest()) as foo;
$$
LANGUAGE sql;
更新语句:
UPDATE deals SET remaining = max - array_sum(hours), until = min - array_sum(hours);