oracle将多行字段拼接成一个列 (listagg函数)

发布时间 2023-09-25 21:13:38作者: 每天进步多一点

1、基本语法

将多行同列合并展示为一行:LISTAGG(展示字段,分隔符) WITHIN GROUP( ORDER BY 分组维度)

2、解析

with temp as(
select '中国' country ,'北京' city from dual 
union all
select '中国' country ,'深圳' city from dual 
union all
select '中国' country ,'上海' city from dual 
)
select country,listagg(city,',') within GROUP (order by country) as brands
from temp
group by country