Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Joining column values

Posted on 2011-09-30
10
Medium Priority
?
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 400 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 77

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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 1600 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 77

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 77

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

598 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