sql - 数据库无法识别创建的函数? SQL状态: 42883

标签 sql postgresql function coalesce greenplum

NULL 我的函数已成功创建,但是当我尝试使用它时,我收到错误消息:

ERROR: function coalesce2(character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying, character varying) does not exist SQL state: 42883 Hint: No function matches the given name and argument types. You may need to add explicit type casts.

我的函数的目的是获取第一个 NON NULL 值并将其与其右侧的值进行比较。如果右侧的值不为 NULL,那么我将使用该值,如果是,那么我将使用 COALESCE 值。我的功能如下:

CREATE OR REPLACE FUNCTION coalesce2(variadic anyarray)
RETURNS anyelement AS
$BODY$
BEGIN
FOR i IN 1 .. array_upper($1, 1)
LOOP
IF $1[i] IS NOT NULL THEN
  RETURN COALESCE($1[i+1], $1[i]);
END IF;
END LOOP;
RETURN NULL;  
END
$BODY$
LANGUAGE plpgsql IMMUTABLE;

这是正在使用的函数:

coalesce2 ( vw_header_to_node_13.subsetname, vw_header_to_node_12.subsetname, vw_header_to_node_11.subsetname, vw_header_to_node_10.subsetname, vw_header_to_node_9.subsetname, vw_header_to_node_8.subsetname, vw_header_to_node_7.subsetname, vw_header_to_node_6.subsetname, vw_header_to_node_5.subsetname, vw_header_to_node_4.subsetname, vw_header_to_node_3.subsetname, vw_header_to_node_2.subsetname, vw_header_to_node_1.subsetname, vw_header_to_node.subsetname, vw_header_to_node.setname) AS prctr2

我对函数没有太多经验,我不明白为什么它不能识别新创建的函数。非常感谢任何建议。

最佳答案

您需要将一个数组传递给您的函数。

SELECT coalesce2(ARRAY['one', 'two', 'three']);

但是这个功能会很贵!我不建议像这样在内联 SQL 中使用 PL/pgSQL 函数。您最好使用 CASE 语句并创建一个不依赖于使用函数来格式化数据的新表。

并回答关于如何使用 CASE 语句的第二个问题。

注意:你的 SQL 示例让我担心,因为看起来你正在执行 10 个 LEFT OUTER JOIN 语句,这是昂贵的。它还以“vw_”为前缀,这让我认为您有 10 个 View ,并且 View 也可以隐藏非常糟糕的 SQL。

我希望您没有使用 VIEWS 和大量 LEFT OUTER JOIN 语句。分析最好使用一个大的平面表,其中包含您需要的每个属性,并且是面向列存储的,或者使用经典的星型模式。转换数据一次,然后使用该输出进行分析。

继续回答。这是一个示例表,其中包含一些与您的数据类似的数据:

    drop table if exists foo;
    create table foo
    (id int not null,
     col1 text,
     col2 text,
     col3 text,
     col4 text,
     col5 text,
     col6 text,
     col7 text,
     col8 text,
     col9 text,
     col10 text)
     distributed by (id);

    insert into foo (id, col1, col2) values (1, 'x1', 'x1');
    insert into foo (id, col2, col3) values (2, 'x2', 'x2');
    insert into foo (id, col3, col4) values (3, 'x3', 'x3');
    insert into foo (id, col4, col5) values (4, 'x4', 'x4');
    insert into foo (id, col5, col6) values (5, 'x5', 'x5');
    insert into foo (id, col6, col7) values (6, 'x6', 'x6');
    insert into foo (id, col7, col8) values (7, 'x7', 'x7');
    insert into foo (id, col8, col9) values (8, 'x8', 'x8');
    insert into foo (id, col9, col10) values (9, 'x9', 'x9');
    insert into foo (id, col10) values (10, 'x10');

这就是 CASE 语句的样子:

    select id, case when col1 is not null then coalesce(col2, col3, col4, col5, col6, col7, col8, col9, col10) 
            when col2 is not null then coalesce(col3, col4, col5, col6, col7, col8, col9, col10) 
            when col3 is not null then coalesce(col4, col5, col6, col7, col8, col9, col10) 
            when col4 is not null then coalesce(col5, col6, col7, col8, col9, col10)
            when col5 is not null then coalesce(col6, col7, col8, col9, col10) 
            when col6 is not null then coalesce(col7, col8, col9, col10) 
            when col7 is not null then coalesce(col8, col9, col10)
            when col8 is not null then coalesce(col9, col10)
            when col9 is not null then coalesce(col10)
            else col10 end
    from foo
    order by id;

现在作为 SQL 函数(不是 PL/pgSQL):

    create or replace function fn_coalesce2(text, text, text, text, text, text, text, text, text, text) returns text as
    $$
    select  case when $1 is not null then coalesce($2, $3, $4, $5, $6, $7, $8, $9, $10) 
            when $2 is not null then coalesce($3, $4, $5, $6, $7, $8, $9, $10) 
            when $3 is not null then coalesce($4, $5, $6, $7, $8, $9, $10) 
            when $4 is not null then coalesce($5, $6, $7, $8, $9, $10)
            when $5 is not null then coalesce($6, $7, $8, $9, $10) 
            when $6 is not null then coalesce($7, $8, $9, $10) 
            when $7 is not null then coalesce($8, $9, $10)
            when $8 is not null then coalesce($9, $10)
            when $9 is not null then coalesce($10)
            else $10 end;
    $$
    language sql;

    select id, fn_coalesce2(col1, col2, col3, col4, col5, col6, col7, col8, col9, col10)
    from foo
    order by id;

    id | fn_coalesce2 
    ----+--------------
      1 | x1
      2 | x2
      3 | x3
      4 | x4
      5 | x5
      6 | x6
      7 | x7
      8 | x8
      9 | x9
     10 | x10
    (10 rows)

关于sql - 数据库无法识别创建的函数? SQL状态: 42883,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33423894/

相关文章:

mysql - 如何在 'USE database' 语句中使用用户变量

mysql - 如何在MYSQL中编写类似 "shortest path"的命令或至少 "find a path"命令?

postgresql - 如何在一个表中创建多个序列?

python-3.x - psycopg2.DataError : invalid input syntax for type double precision: "NULL"

c - 我如何使指针工作?

c - 在c中打印多维数组

function - 确保kotlin方法是静态的,顶级的或带注释的@JvmStatic

mysql - 将 AVG(整数) 与类别的 AVG(整数) 进行比较

sql - oracle中的子选择

sql - 递归 CTE 来遍历网络,但根据属性选择交叉路口的路径