oracle 怎样实现合并相同行 希望是一条语句

create table tb
(
  id number,  
  name varchar2(100)
);
insert into tb values(1,'aa');
insert into tb values(2,'bb');
insert into tb values(3,'cc');
insert into tb values(1,'dd');
insert into tb values(1,'ee');实现 
1    aa,dd,ee
2    bb
3    cc    

解决方案 »

  1.   

    create or replace function t_f                                                                    
    (                                                                               
      v_id number                                                                   
    )                                                                               
    return varchar2                                                                 
    is                                                                              
      result varchar2(5000);                                                        
    begin                                                                           
     result := '';                                                                  
     for x in (select name from tb where id = v_id)                                 
     loop                                                                           
         result := result || x.name || ',';                                         
     end loop;                                                                       result := substr(result, 1, length(result) - 1);                               
     return result;                                                                 
    end t_f;              
    select  id, t_f(id) as name from (select distinct id from tb) x