Home » SQL & PL/SQL » SQL & PL/SQL » Re: Concatenate Column Values from Multiple Rows into a Single Column
Re: Concatenate Column Values from Multiple Rows into a Single Column [message #9644] Tue, 25 November 2003 22:35 Go to next message
Yaseen
Messages: 3
Registered: May 2002
Junior Member
Thanks Maheer and Adrian,
The problem is work in restricted environment. I use a tool developed by our company to execute the sql. I cannot create functions in my environment. The database we use is 9i. Is there any built in funtion in 9i for the same?

Thanks & Regards,
Yaseen.
Re: Concatenate Column Values from Multiple Rows into a Single Column [message #9648 is a reply to message #9644] Wed, 26 November 2003 00:09 Go to previous message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
If you know what the maximum possible rows are, you can do it in plain SQL. It involves constructing a DECODE like below:
SQL> Select count(*) nbr
  2       , deptno
  3    From emp
  4   Group by deptno;

       NBR     DEPTNO
---------- ----------
         3         10
         5         20
         6         30

SQL> select deptno
  2       , max(decode(my_seq,1,rtrim(ename)))||
  3         max(decode(my_seq,2,nvl2(ename,', '|| rtrim(ename),null)))||
  4         max(decode(my_seq,3,nvl2(ename,', '|| rtrim(ename),null)))||
  5         max(decode(my_seq,4,nvl2(ename,', '|| rtrim(ename),null)))||
  6         max(decode(my_seq,5,nvl2(ename,', '|| rtrim(ename),null)))||
  7         max(decode(my_seq,6,nvl2(ename,', '|| rtrim(ename),null))) emp_list
  8    from ( select deptno
  9         , ename
 10         , row_number() over (partition by deptno order by ename) my_seq
 11          from emp
 12          )
 13    group by deptno
 14    order by 1
 15  /

    DEPTNO EMP_LIST
---------- ----------------------------------------------------------------------
        10 CLARK, KING, MILLER
        20 ADAMS, FORD, JONES, SCOTT, SMITH
        30 ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD


But I would like to suggest this solution. It involves creating a function, but it is reusable. The followups are useful too.
Previous Topic: Trigger : Need help coding the error condition
Next Topic: Trigger : need help with coding the error condition
Goto Forum:
  


Current Time: Wed Apr 24 14:42:13 CDT 2024