How to apply function on multiple tuples in postgres

问题描述 投票:0回答:1

我有一个包含两列数组的视图。

select * from cj_of_skills

我想找到我有一个函数的两列的并集

create or replace function set_union(A anyarray, B anyarray) 
returns anyarray as
$$
   select array(select unnest(A) union select unnest(B) order by 1);
$$ language sql;

我想将函数应用于视图,但出现以下错误

select * from set_union((select s1 from cj_of_skills ), (select s2 from cj_of_skills)) 

错误:用作表达式的子查询返回多于一行

sql postgresql function view
1个回答
0
投票

以下函数返回由两个数组构建的有序集合。如果任一参数为 NULL,则结果将为 NULL。

CREATE OR REPLACE FUNCTION set_union (a anyarray, b anyarray)
  RETURNS anyarray STRICT IMMUTABLE
  LANGUAGE SQL
  AS $FUNC$
  SELECT
    array_agg(element ORDER BY element)
  FROM (
    SELECT
      element
    FROM
      unnest(a) a1 (element)
    UNION
    SELECT
      element
    FROM
      unnest(b) a2 (element)) t;
$FUNC$;
© www.soinside.com 2019 - 2024. All rights reserved.