将oracle 查询结果列拼接为字符串
发布网友
发布时间:2022-04-08 01:32
我来回答
共3个回答
热心网友
时间:2022-04-08 03:02
create table test (id int, name varchar(10) )
insert into test values (1,'a')
insert into test values (1,'b')
insert into test values (1,'c')
insert into test values (2,'a')
insert into test values (2,'b')
insert into test values (3,'a')
insert into test values (3,'c')
select id,sys_connect_by_path(name,',') from (
select id,name, row_number() over(partition by id order by name)rn,
count(*) over(partition by id ) cnt from test
) a where level=cnt
start with rn=1 connect by prior id=id and prior rn=rn-1
测试后 可用。
一楼的回答其实最容易理解了。你把它修改成动态sql 就可以了。可以不受*了。
热心网友
时间:2022-04-08 04:20
个数有限,可以实现
select id, max(case when rk = 1 then name else '' end)
||','||max(case when rk = 2 then name else '' end)
||','||max(case when rk = 3 then name else '' end)
from (SELECT id, name,row_number() over(partition by id order by name) rk from tab) t
group by id追问个数是不无限的,具体的值不确定,是根据关联关系出来的
追答那自己写存储过程吧,sql不是万能的。
热心网友
时间:2022-04-08 05:54
select t.id, WMSYS.WM_CONCAT(t.Name) name From table_name t GROUP BY t.id追问中文会出现乱码,咋解决