我正在尝试创建一个 postgresql.但是我一直有错误
ERROR: operator does not exist: integer[] <@ integer Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
CREATE OR REPLACE FUNCTION accumulate_cs_order(int[][], int)
returns int[][]
language sql
as $f$
SELECT CASE
WHEN array[abs($2)] <@ $1[1] THEN -- If $2 has been closed pass
$1
WHEN $2 < 0 THEN -- elIf we're closing $2 add it to $1[1]
array[array_append($1[1], abs($2)), $1[2]]
WHEN $2 < $1[2] THEN -- ElIf we're not closing $2 and it is cheaper than $1[2], this is our new CS
array[$1[1], array[$2]]
ELSE
$1
END;
$f$;
我不明白为什么它不起作用。我已经用正确的类型声明了我的变量,array[abs($2)] <@ $1[1] 对我来说很有意义,因为 $1[1] 是一个数组,但 postgresql 似乎认为它是一个 int。
最佳答案
它与如何获取特定元素与数组切片有关。一片
WHEN abs($2) = ANY($1[1:1]) THEN
应该可以,但是:
select (array[array[1,2,3],array[4,5,6],array[7,8,9]])[1:1];
array
-----------
{{1,2,3}}
它是整个数组的矩形切片,而不仅仅是一维数组。
select (array[array[1,2,3],array[4,5,6],array[7,8,9]])[1:1] = array[1,2,3];
?column?
----------
f
关于sql - 在 postgresql 函数中使用数组运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35044895/