sql - 在 postgresql 函数中使用数组运算符

标签 sql arrays postgresql function

我正在尝试创建一个 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/

相关文章:

mysql - 导入脚本时未选择数据库

MySQL设计问题

mysql - 如何使用聚合函数进行左连接?

Java 使用 while 循环将用户输入加载到数组中

javascript - 从对象中获取值

c++ - C++ 中矩阵类的性能

php - Google map 商店定位器 - SQL 语句的 PHP/MySQL 错误

sql - 如何创建一个插入但随后返回插入数据的事务

java - 由于类型无效,无法 hibernate @ManyToMany

postgresql - 在 postgresql 中通过批处理文件运行查询