select id,string_agg(name,',') AS NAME_NEW from test
group by 1
ORDER BY 1
方法二:
array_agg(字段名)
select id,array_agg(name)
AS NAME_NEW from test
group by 1
ORDER BY 1;
select id,array_to_string(array_agg(name),',')
AS NAME_NEW from test
group by 1
ORDER BY 1;
另外:array_agg(distinct(字段名)) 拼接唯一的字段。
一行变多行
select id,regexp_split_to_table(name_new,',')
AS NAME from test;
select id,unnest(string_to_array(name_new,','))
AS NAME from test;