Group_concat equivalent in postgresql 8.2.11

北慕城南 提交于 2019-12-13 05:42:17

问题


I am using a older version of Postgres 8.2.11. Can anyone tell me the equivalent of MySql's group_concat for this Postgres 8.2.11. I have tried array_accum , array_to_string , string_agg but it doesn't work in this version


回答1:


The "not quite duplicate" in the comments should point you in the right direction: create your own aggregate function. First you'll need a non-aggregate string concatenation function, something like this:

create function concat(t1 text, t2 text) returns text as $$
begin
    return t1 || t2;
end;
$$ language plpgsql;

Then you can define your own aggregate version of that function:

create aggregate group_concat(
    sfunc    = concat,
    basetype = text,
    stype    = text,
    initcond = ''
);

Now you can group_concat all you want:

select group_concat(s)
from t
group by g

I dug this out of my archives but I think it should work in 8.2.

Keep in mind that 8.2 is no longer supported so you might want to upgrade to at least 8.4 as soon as possible.



来源:https://stackoverflow.com/questions/16455483/group-concat-equivalent-in-postgresql-8-2-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!