【问题标题】:Intersection of multiple text arrays: ERROR: array value must start with "{"多个文本数组的交集:错误:数组值必须以“{”开头
【发布时间】:2015-12-08 21:09:10
【问题描述】:

我正在尝试让这个问题中的功能起作用:Intersection of multiple arrays in PostgreSQL

与那个问题不同,我想交叉文本数组而不是整数数组。我已经相应地修改了这两个函数。基数组相交函数:

CREATE FUNCTION array_intersect(a1 text[], a2 text[]) RETURNS text[] AS $$
DECLARE
    ret text[];
BEGIN
    IF a1 is null THEN
        return a2;
    ELSEIF a2 is null THEN
        RETURN a1;
    END IF;
    SELECT array_agg(e) INTO ret
    FROM (
        SELECT unnest(a1)
        INTERSECT
        SELECT unnest(a2)
    ) AS dt(e);
    RETURN ret;
END;
$$ language plpgsql;

聚合函数定义:

CREATE AGGREGATE utility.array_intersect_agg(
    sfunc    = array_intersect,
    basetype = text[],
    stype    = text[],
    initcond = NULL
);

我收到错误“错误:数组值必须以“{”或维度信息开头 SQL state: 22P02" 当我尝试运行以下代码时:

SELECT array_intersect_agg(test)
  FROM(
    SELECT ARRAY['A','B','C'] test
    UNION ALL
    SELECT ARRAY['A','C'] test
    ) a

为了使这些功能起作用,需要进行哪些更改?

【问题讨论】:

    标签: sql postgresql aggregate-functions intersection postgresql-8.4


    【解决方案1】:

    对于the documentation

    初始条件

    状态值的初始设置。这必须是一个字符串 数据类型 state_data_type 接受的形式中的常量。如果 未指定,状态值开始为 null。

    所以聚合声明应该是这样的:

    CREATE AGGREGATE array_intersect_agg(
        sfunc    = array_intersect,
        basetype = text[],
        stype    = text[]
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      相关资源
      最近更新 更多