sql - WM_CONCAT duplicates removal -


i have table below.

   cola       colb    12345      nhs,cde,bcd    12345      nhs,abc,def 

need display data in below format

   cola       colb    12345      nhs,abc,bcd,cde,def 

i need generalized solution remove duplicate nhs, comes first, , put remaining words in alphabetical order.

firstly, never use wm_concat since undocumented , no more available in latest version 12c. see why not use wm_concat function in oracle? , why wm_concat not work here?

since on 11g, use listagg.

the below query following things:

  1. split comma delimited string rows.
  2. apply string aggregation using listagg.
  3. case expression handle custom ordering.

for example,

sql> data as(   2      select 12345 cola, 'nhs,cde,bcd' colb dual union   3      select 12345 cola, 'nhs,abc,def' colb dual   4      )   5  select cola,   6    listagg(colb, ',') within group(   7  order   8    case colb   9      when 'nhs'  10      1  11      else 2  12    end, colb) colb  13   14    (select distinct cola,  15      trim(regexp_substr(colb, '[^,]+', 1, level)) colb  16    data  17      connect level <= regexp_count(colb, ',')+1  18    order colb  19    )  20  group cola  21  /        cola colb ---------- ------------------------------      12345 nhs,abc,bcd,cde,def 

edit @alexpoole pointed out, explicit ordering missing , previous query(see edit history) relied on distinct ordering of values.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -