Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Concatenar e agrupar várias linhas no Oracle


Considere usar a função LISTAGG caso você esteja no 11g:
select grp, listagg(name,',') within group( order by name ) 
  from name_table group by grp

sqlFiddle

atualização: Caso não esteja, considere usar análises:
select grp,
    ltrim(max(sys_connect_by_path
       (name, ',' )), ',')
        scbp
  from (select name, grp,
            row_number() over
           (partition by grp
            order by name) rn
         from tab
          )
start with rn = 1
connect by prior rn = rn-1
and prior grp = grp
  group by grp
  order by grp

sqlFiddle