如何在使用PostgreSQL创建聚合函数时在sfunc参数中使用Concat函数

时间:2011-09-22 07:58:25

标签: postgresql

我最近倾向于使用PostgreSQL并尝试将ListAggregation函数设置为给定here,唯一的区别是我尝试使用CONCAT而不是TextCat 。 我的功能如下

CREATE AGGREGATE ListAggregation(
      basetype    = Text,
      sfunc       = Concat,
      stype       = Text,
      initcond    = ''
  );

抛出错误

ERROR:  function concat(text, text) does not exist

********** Error **********

ERROR: function concat(text, text) does not exist
SQL state: 42883

我犯了什么错误......请帮忙

N.B.~我甚至看过给出here

的例子

由于

2 个答案:

答案 0 :(得分:1)

有趣的是,你打算做什么? PostgreSQL 9.0 +中已经有string_agg()聚合函数...

答案 1 :(得分:0)

您应该创建状态更改函数 sfunc 以实现具有签名的聚合: sfunc(state,value)--->下一状态

-- sfunc:
CREATE OR REPLACE FUNCTION concat(text, text) 
RETURNS text 
LANGUAGE SQL 
AS $$
  SELECT $1||$2;
$$;

-- Aggregate:
CREATE AGGREGATE ListAggregation(
      basetype    = text,
      sfunc       = concat,
      stype       = text,
      initcond    = ''
);

-- Testing:
WITH test(v) AS (VALUES
  ('AAAA'),
  ('BBBB'),
  ('1111'),
  ('2222') )
SELECT ListAggregation(v) FROM test;