Solved

Joining column values

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PL/SQL Display based on value 4 26
SQL query question 8 73
clob to char in oracle 3 34
make null the repeated levels 2 16
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…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

785 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