Solved

Oracle 10g - Concatonate multiple rows

Posted on 2013-05-17
8
524 Views
Last Modified: 2013-05-17
Experts,

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:
Select
Line_num,
Line_text 
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?
0
Comment
Question by:JDCam
  • 4
  • 3
8 Comments
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 250 total points
Comment Utility
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');
commit;


Select
Line_num,
Line_text 
from tab1
where CompCode = 'A1'
and Ord_num = 628649
/


select 
 TRIM(
           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

0
 
LVL 31

Accepted Solution

by:
awking00 earned 250 total points
Comment Utility
slightwv,
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;

TRIM(EXTRACT(XMLAGG(XMLELEMENT("S",LINE_TEXT||'')ORDERBYLINE_NUMASC),'/S/TEXT()'
--------------------------------------------------------------------------------
SO
 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;

EXTRACT(XMLAGG(XMLELEMENT("S",LINE_TEXT)ORDERBYLINE_NUMASC),'/S/TEXT()').GETCLOB
--------------------------------------------------------------------------------
SO
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.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>>which may be more desirable.

Good point!
0
 

Author Closing Comment

by:JDCam
Comment Utility
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
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 31

Expert Comment

by:awking00
Comment Utility
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.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
It's OK awking00,

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

Expert Comment

by:awking00
Comment Utility
Maybe we can bring Sean in on the part 2 question so he can get his due :-)
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
Let him fend for himself!!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

762 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

6 Experts available now in Live!

Get 1:1 Help Now