Oracle 10g - Concatonate multiple rows

Posted on 2013-05-17
Last Modified: 2013-05-17

A table contains text comments for an order. During input, the application breaks the text at  60 char and inserts as multiple rows. Each row is given a sequential line number.

I need to output this data in a report, but doing so as-is looks very broken and choppy.  I want to use a query to concatenate the rows into one and pass that into the report designer where it can be properly formatted

A simple Select looks like this:
from Ord_Comment 
where CompCode = ‘A1’
and Ord_num = 628649

Open in new window

Output looks like
1	SO
2	CS Hold 5 Items are being ordered in less tha
3	n Full
4	Master Case Qty. Emailed customer to revise P
5	O. JO 4-23

Open in new window

Desired output is a single column, single row with all Line_Text concatenated. Note:  there are CR/LF after row 1 and 3, the character needs to remain in the concatenated string

Can this be done in a standard query without creating a user defined function?
Question by:JDCam
  • 4
  • 3
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 250 total points
ID: 39174761
Try this:

drop table tab1 purge;
create table tab1(compCode char(2), ord_num number, line_num number, line_text varchar2(50));

--add a CHR(10) at the end of line 1 and 3
insert into tab1 values('A1',628649,1,'SO' || chr(10));
insert into tab1 values('A1',628649,2,'CS Hold 5 Items are being ordered in less tha');
insert into tab1 values('A1',628649,3,'n Full' || chr(10));
insert into tab1 values('A1',628649,4,'Master Case Qty. Emailed customer to revise P');
insert into tab1 values('A1',628649,5,'O. JO 4-23');
--dummy row justs for grins
insert into tab1 values('ZZ',12345,1,'Hello World');

from tab1
where CompCode = 'A1'
and Ord_num = 628649

           EXTRACT(XMLAGG(XMLELEMENT("s", line_text || ' ') order by line_num asc), '/s/text()').getclobval()
from tab1
where CompCode = 'A1'
   and Ord_num = 628649
group by compCode,ord_num;

Open in new window

LVL 32

Accepted Solution

awking00 earned 250 total points
ID: 39174944
I think concatenating the space will produce results like -
SQL> select
  2   TRIM(
  3             EXTRACT(XMLAGG(XMLELEMENT("s", line_text||' ') order by line_num
 asc), '/s/text()').getclobval()
  4         )
  5  from tab1
  6  where CompCode = 'A1'
  7     and Ord_num = 628649
  8  group by compCode,ord_num;

 CS Hold 5 Items are being ordered in less tha n Full
 Master Case Qty. Emailed customer to revise P O. JO 4-23
Where there is a space between the 'tha' and 'n' plus a space after each line feed.

Removing the space concatenation(which also removes the need for the trim function) would produce results like this -
SQL> select
  2             EXTRACT(XMLAGG(XMLELEMENT("s", line_text) order by line_num asc)
, '/s/text()').getclobval()
  3  from tab1
  4  where CompCode = 'A1'
  5     and Ord_num = 628649
  6  group by compCode,ord_num;

CS Hold 5 Items are being ordered in less than Full
Master Case Qty. Emailed customer to revise PO. JO 4-23

which may be more desirable.
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39174958
>>which may be more desirable.

Good point!
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Closing Comment

ID: 39175747
Excellent guys.  I will share the points evenly.

I want to make a slight shange to this request. I will repost as a new question in a few moments.  Thanks
LVL 32

Expert Comment

ID: 39175763
I think slightwv should get the lion's share of the points since it was really his solution and I just pointed out a small anomaly.
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39175779
It's OK awking00,

The flaw you found was just as important and to be honest, that core code was borrowed from sdstuber!
LVL 32

Expert Comment

ID: 39175811
Maybe we can bring Sean in on the part 2 question so he can get his due :-)
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39175819
Let him fend for himself!!!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Access join syntax when converting to T-SQL query 4 43
subtr returning incorrect value 8 63
Comparison query - 4 columns 9 42
SQL Syntax 6 36
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

685 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