Solved

Joining column values

Posted on 2011-09-30
10
383 Views
Last Modified: 2012-05-12
Hello

Is it possible to join strings together in a column - eg
say my data set is as

Col1               Col2            
A                    Test1
A                    Test2
B                    Test3
B                    Test4
....what I would like
is
Col1               Col2
A                   Test1 Test2
B                   Test3 Test4
Note: there could be more than 2 rows per grouping as in A & B - probably max of 5

Regards
0
Comment
Question by:philsivyer
  • 5
  • 4
10 Comments
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 100 total points
ID: 36891156
use function


CREATE OR REPLACE FUNCTION get_employees (p_deptno  in  emp.deptno%TYPE)
  RETURN VARCHAR2
IS
  l_text  VARCHAR2(32767) := NULL;
BEGIN
  FOR cur_rec IN (SELECT ename FROM emp WHERE deptno = p_deptno) LOOP
    l_text := l_text || ',' || cur_rec.ename;
  END LOOP;
  RETURN LTRIM(l_text, ',');
END;
/


Call

SELECT deptno,
       get_employees(deptno) AS employees
FROM   emp
GROUP by deptno;


In your case

Select col1 ,get_employees(col2)
FROM   tablename
GROUP by col1;

refer
http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36891363
In 11gR2 there is a listagg functuon to do this.

I also suggest the XMLAGG method since it doesn't require and procedural code.

If you search this site you will find examples.  I'm on mobile right now and cannot find the link.

I will later if you cannot find an example.
0
 

Author Comment

by:philsivyer
ID: 36899044
Hello
Would be interested in examples
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 400 total points
ID: 36903022
Check out below.
drop table tab1 purge;
create table tab1(col1 char(1), col2 varchar2(5));

insert into tab1 values('A','Test1');
insert into tab1 values('A','Test2');
insert into tab1 values('B','Test3');
insert into tab1 values('B','Test4');
commit;

--XML example from: http://www.experts-exchange.com/Database/Oracle/Q_24914739.html?#a25864822
SELECT col1,
       RTRIM(
           EXTRACT(XMLAGG(XMLELEMENT("s", col2 || ' ')), '/s/text()').getstringval(),
           ' '
       )
           col2
FROM tab1
GROUP BY col1
/


select col1, listagg(col2, ' ') within group (order by col2)
from tab1
group by col1
/

Open in new window

0
 

Author Comment

by:philsivyer
ID: 36910052
Hello

The xmlagg works a treat - the .....
select col1, listagg(col2, ' ') within group (order by col2)
from tab1
group by col1

throws an error......
FROM keyword not found where expected
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36910071
Are you running 11gR2?  Listagg is new for that release.
0
 

Author Comment

by:philsivyer
ID: 36922988
Yes - running 11gR2
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36924102
I ran the code I posted as-is in 11gR2 sqlplus.  I just ran it again just in case I pasted the wrong code and it ran fine.
0
 

Author Comment

by:philsivyer
ID: 36929604
Thanks Guys
0
 

Author Closing Comment

by:philsivyer
ID: 36929615
Using the XMLAGG - Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now