Aggregate String Concatenation in Oracle 10g

14,169

Solution 1

It is easy on 11G, you can use the LISTAGG function, but sadly not on 10G

There are some techniques here for earlier versions however they do require a function to be written.

http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php

Solution 2

Try this query

select No  , rtrim(Name,',') Name
   from ( select No , Name , rn from yourtable
           model
  partition by (No)
  dimension by (row_number() over
 (partition by No order by Name) rn
 )
 measures  (cast(Name as varchar2(40)) Name)
   rules
 ( Name[any] order by rn desc = Name[cv()]||' '||Name[cv()+1]
   )
  )
    where rn = 1
    order by NO

Here is your sql demo

Share:
14,169

Related videos on Youtube

Arjun Vasudevan
Author by

Arjun Vasudevan

Programmer Analyst, Spiritual Seeker, Undercover Economist

Updated on October 10, 2022

Comments

  • Arjun Vasudevan
    Arjun Vasudevan over 1 year

    I'm using Oracle 10 g, I have a scenario similar to this:

    No Name
    -- -----
    1 Rony
    1 James
    1 Aby
    2 Sam
    2 Willy
    3 Mike

    I need to aggregate and concatenate the strings (with a single space in between), in a way to get the results:

    No Name
    -- -----
    1 Rony James Aby
    2 Sam Willy
    3 Mike

    I'm using Oracle 10g and have to implement this using SQL and not PL/SQL. Is there a way out?

    • Dinup Kandel
      Dinup Kandel almost 11 years
      @Arjun did you find you solution. I have modified my answer to match for 10g.
  • Noel
    Noel almost 11 years
    LISTAGG is not supported in oracle10g
  • Dinup Kandel
    Dinup Kandel almost 11 years
    @Horrendous_Space_Kablooie yes thanks i thought it was 11g. now its done.
  • Frank Myat Thu
    Frank Myat Thu almost 11 years
    STUFF keyword is great, thank @Msyma.
  • Billy Moon
    Billy Moon over 10 years
    Is it possible to turn this into a reusable function, to permit usage like... select no, gconcat(name) from mytable;?
  • Admin
    Admin almost 10 years
    This became available with 11g.2.