可以用存储过程来实现:
create or replace procedure aa as
cursor cur1 is
  select distinct b from tableA;
cursor cur2 is
  select a,b from tableA;
str  varchar2(n):='';
begin 
  for i in cur1 loop
      for t in cur2 loop
          if t.b=i.b then
             str:=str||t.a;
          end if;
        insert into tableB values(str,t.b);
       end loop;
   str:='';
  end loop;
end;