postgresql

--拆
with tmp_table as (
select
	'a' name1,
	'A,B,C,D'::varchar as t )
select
	name1,
	regexp_split_to_table(t, ',') t
from
	tmp_table;
	
--合
select 
	name1,
	string_agg(t,',' order by t) t 
from (
	select 'a' name1,'A' t
	union all 
	select 'a' name1,'B' t
	union all 
	select 'a' name1,'C' t
	union all 
	select 'a' name1,'D' t
) a
group by name1;

oracle

--拆
with tmp_table as (
    select
		 'a' name1
		,'A,B,C,D' as t 
	FROM dual
)
SELECT name1,regexp_substr(t,'[^,]+',1,LEVEL) t FROM tmp_table a
CONNECT BY LEVEL <= regexp_count(a.t,'\,\')+1;

--合
SELECT 
    name1,
    listagg(t,',')WITHIN GROUP(ORDER BY t) t 
FROM (
    select 'a' name1,'A' t FROM dual
    UNION ALL 
    select 'a' name1,'B' t FROM dual
    UNION ALL 
    select 'a' name1,'C' t FROM dual
    UNION ALL 
    SELECT 'a' name1,'D' t FROM dual
) a
GROUP BY name1;
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐