Solved

Joining column values

Posted on 2011-09-30
10
384 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

932 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

12 Experts available now in Live!

Get 1:1 Help Now