简体   繁体   中英

oracle distinct with listagg

I have a table. I can show all the data of a colomn in my table using ',' in the same line. but I can't apply it distinctly. hepl please

This is tricky. One simple suggestion is to use select distinct :

select listagg(col, ',') within group (order by col)
from (select distinct col from t) x;

However, that makes it difficult to calculate other aggregations (or to generate more than on listagg() result). Another way is to use window functions in combination with listagg() :

select listagg(case when seqnum = 1 then col end, ',') within group (order by col)
from (select t.*,
             row_number() over (partition by col order by col) as seqnum
      from t
     ) t

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM