Postgresql - 坚持构建 plpgsql 函数

Postgresql - Stuck building a plpgsql function

我是 plpgsql 的初学者,我被困在编写一个函数中。 我需要一个执行以下操作的函数:

在 table 中给出一个 table,在 table 中给出一个 id_field,在 table 中给出另一个字段:

原创TABLE

id_field     field_traspose
---------    --------------
    1              A
    1              B
    1              C
    2              A
    3              F
    3              X

结果

id_field     field_traspose
---------    --------------
    1              A, B, C
    2              A
    3              F, X

我的尝试:

CREATE OR REPLACE FUNCTION traspose(mytable character varying, id_field character varying, field_traspose character varying)
  RETURNS setof RECORD AS
$BODY$ 
DECLARE
    r record;
    result record;
BEGIN

FOR r IN EXECUTE 'SELECT '||id_field||','||field_traspose||'  from '||mytable LOOP

-- Here should go the logic that joins every field_traspose for a same id_field and
--returns the record as one of the returning records (can be many different id_fields)   

RETURN NEXT result;
END LOOP;

RETURN;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

我卡在这一点上了。 此致,

不需要这样的功能,这个已经内置了:

select id_field, 
       string_agg(field_traspose, ', ' order by field_traspose) 
from the_table 
group by id_field
order by id_field;