Ordering the strings while concatenating in oracle

后端 未结 1 1334
夕颜
夕颜 2021-01-23 10:03

I am using the collect function to concatenate strings for a sql query.

    select id,
    tab_to_string(CAST(COLLECT(a.level||\' \'||d.Number||\':  
    \'||to_c         


        
相关标签:
1条回答
  • 2021-01-23 10:14

    to get an ordered list, there's a few ways. the simplest is :

    select id, str
      from (select id, 
                   wm_concat('level : ' || to_char(nvl(eventDate,SYSDATE - 365 * 100))) 
                     over (partition by id order by eventdate) str,
                   row_number() over (partition by id order by eventdate desc) rn
             from Mytable)
     where rn = 1;
    

    or if you're using the "stragg" user defined aggregate:

      select id, str
      from (select id, 
                   string_agg('level : ' || to_char(nvl(eventDate,SYSDATE - 365 * 100))) 
                     over (partition by id order by eventdate) str,
                   row_number() over (partition by id order by eventdate desc) rn
             from Mytable)
     where rn = 1;
    

    eg

    SQL> select id, str
      2    from (select id,
      3                 string_agg('level : ' || to_char(nvl(eventDate,SYSDATE - 365 * 100)))
      4                   over (partition by id order by eventdate) str,
      5                 row_number() over (partition by id order by eventdate desc) rn
      6           from Mytable)
      7   where rn = 1;
    
            ID STR
    ---------- ----------------------------------------------------------------------
             1 level : 27-MAR-08,level : 27-JAN-09,level : 02-APR-10
             2 level : 07-JUN-06,level : 02-NOV-08,level : 27-DEC-08
             3 level : 27-MAR-06,level : 02-APR-10,level : 27-JUL-10
    
    0 讨论(0)
提交回复
热议问题