Write a query to Sort the numbers in comma seperated

Input:

4,10,6,1,8,46,52,12,18

Output:

1,4,6,8,10,12,18,46,52

STEP1:

SELECT  '4,10,6,1,8,46,52,12,18' c FROM  dual;


STEP2:

with d as (SELECT  '4,10,6,1,8,46,52,12,18' c FROM  dual)

    select c from d

    connect by level<=regexp_count(c,',')+1;

Output:

4,10,6,1,8,46,52,12,18
4,10,6,1,8,46,52,12,18
4,10,6,1,8,46,52,12,18
4,10,6,1,8,46,52,12,18
4,10,6,1,8,46,52,12,18
4,10,6,1,8,46,52,12,18
4,10,6,1,8,46,52,12,18
4,10,6,1,8,46,52,12,18
4,10,6,1,8,46,52,12,18

 STEP3:  

with d as (SELECT  '4,10,6,1,8,46,52,12,18' c FROM  dual)

    select c,regexp_substr(c,'[^,]+',1,level) e from d

    connect by level<=regexp_count(c,',')+1;

Output:

4,10,6,1,8,46,52,12,18    4
4,10,6,1,8,46,52,12,18    10
4,10,6,1,8,46,52,12,18    6
4,10,6,1,8,46,52,12,18    1
4,10,6,1,8,46,52,12,18    8
4,10,6,1,8,46,52,12,18    46
4,10,6,1,8,46,52,12,18    52
4,10,6,1,8,46,52,12,18    12
4,10,6,1,8,46,52,12,18    18

 STEP4:  

with d as (SELECT  '4,10,6,1,8,46,52,12,18' c FROM  dual)

    select listagg(e,',') within group(order by e) col from

    (select c,regexp_substr(c,'[^,]+',1,level) e from d

    connect by level<=regexp_count(c,',')+1);

Output:

1,10,12,18,4,46,52,6,8

STEP5:

with d as (SELECT  '4,10,6,1,8,46,52,12,18' c FROM  dual)

    select listagg(e,',') within group(order by to_number(e)) col from

    (select c,regexp_substr(c,'[^,]+',1,level) e from d

    connect by level<=regexp_count(c,',')+1);

Output:

1,4,6,8,10,12,18,46,52


No comments:

Post a Comment