Oracle
 SQL >> Datenbank >  >> RDS >> Oracle

LISTAGG in Oracle für distinkte Werte

19c und neuer:

select listagg(distinct the_column, ',') within group (order by the_column)
from the_table

18c und älter:

select listagg(the_column, ',') within group (order by the_column)
from (select distinct the_column from the_table) t

Brauchst du mehr Spalten, könnte das hier passen:

select col1,
       listagg(col2, ',') within group (order by col2)
from (
  select col1, col2,
         row_number() over (partition by col1, col2 order by col1) as rn
  from foo
  order by col1, col2
)
where rn = 1
group by col1;

Beachte bei beiden Varianten die 4000-Byte-Grenze von VARCHAR2 als Rückgabetyp – bei längeren Listen brauchst du XML-Aggregation mit GetClobVal().