Solved

Joining column values

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

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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Via a live example, show how to take different types of Oracle backups using RMAN.

860 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