小蒜头's Blog

Happy coding

将oracle 查询结果列拼接为字符串

小蒜头 posted @ 2013年4月07日 17:52 in [Oracle数据库] , 3749 阅读

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


 


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter