开发者

How to use Concat function in the sfunc parameter while making a aggregate function using PostgreSQL

开发者 https://www.devze.com 2023-04-06 10:36 出处:网络
I am recently inclined in PostgreSQL and trying to make a ListAggregation function as given here the only difference being tha开发者_如何学编程t I am trying to use CONCAT instead of TextCat

I am recently inclined in PostgreSQL and trying to make a ListAggregation function as given here the only difference being tha开发者_如何学编程t I am trying to use CONCAT instead of TextCat . My function is as under

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

It is throwing error

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

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

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

what mistake I am making...please help

N.B.~ I have even looked at the example given here

Thanks


Interesting, what are you palanning to do? There is already a string_agg() aggregate function in PostgreSQL 9.0+ ...


You should create state change function sfunc to implement an aggregate with the signature: sfunc( state, value ) ---> next-state

-- 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;
0

精彩评论

暂无评论...
验证码 换一张
取 消