简体   繁体   中英

How to order a comma-separated list

In Oracle, I want to rewrite a column the data like this:

2012AName,2019BName,2017DName,2017CName

It's a group by many year+Name order by year desc and name's first char asc after split ',' result like this

2012AName,2017CName,2017CDName,2019BName

Does this fit to your requirement,

First we split the given string and the aggregate back using listagg and the order by you want.

with input(str)
as
(
select'2012AName,2019BName,2017DName,2017CName' as str 
  from dual
)
select listagg(split,',') 
within group (order by substr(split,1,4),substr(split,5,1))
  from 
(
select regexp_substr(str,'[^,]+', 1, level) split
  from input 
connect by regexp_substr(str, '[^,]+', 1, level) 
 is not null
)

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