Oracle
 SQL >> Base de Dados >  >> RDS >> Oracle

Função LISTAGG() no Oracle

No Oracle, LISTAGG() combina valores de múltiplas linhas em uma única linha, com separador opcional e ordenação.

Sintaxe

LISTAGG( [ ALL | DISTINCT ] measure_expr [, 'delimiter']
  [listagg_overflow_clause] )
[ WITHIN GROUP order_by_clause ]
[ OVER query_partition_clause ]

Exemplo

SELECT LISTAGG(region_name, ', ') FROM regions;

Resultado:

Europe, Americas, Asia, Middle East and Africa

Alterar separador

SELECT LISTAGG(region_name, '; ') FROM regions;

Resultado:

Europe; Americas; Asia; Middle East and Africa

Ordenar

SELECT LISTAGG(region_name, ',')
  WITHIN GROUP (ORDER BY region_name ASC)
FROM regions;

Resultado:

Americas,Asia,Europe,Middle East and Africa

Valores distintos

SELECT LISTAGG(DISTINCT region_id, ', ') FROM regions;

Resultado:

1, 2, 3, 4