Solved

sys_connect_by_path subquery

Posted on 2014-07-28
6
422 Views
Last Modified: 2014-07-29
Hi,
I'm not a strong Oracle SQL coder and have a query that is taking far too long to run. What I need is for it as one of the columns to return a concatenated list of values from another table. In brief I have the following:

SELECT
a, b, c,
(select ltrim(max(sys_connect_by_path(rpt_num,',')), ',')
              from (select occ, rpt_num, row_number() over (partition by occ order by rownum) rn from oq)
              connect by occ = prior occand rn = prior rn + 1
              START WITH rn = 1
              group by occ
              HAVING occ= o.occ) Related_Reports
FROM TableO o

a,b, and c are other columns in my query

I suspect that I have messed something up in this, although running the sunquery as a query does return me what I need.

Any suggests or help is most welcome.


Kelvin
0
Comment
Question by:Kelvin Sparks
[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
  • 3
  • 3
6 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40225507
Please provide raw sample data and expected results.

Also full database version (all 4 numbers like 11.2.0.2).
0
 
LVL 22

Author Comment

by:Kelvin Sparks
ID: 40225577
Thanks

Starting from the end - developing with 11.2.0.4, some testing with 11.2.0.3. Will be shipped to customers who could be on varying versions from 10.2.0.3 or later. Our next release will also include 12c (don't know which patch version yet.

Some raw data
Table TableO
Occ   (PK)               Date                  OccTitle
O14-1                   25-01-2014        Title 1
O14-2                   27-01-2014        Title 2
O13-25                15-11-2013         Title 3

Table oq
Occ                     rpt_num
O14-1               ABC123
O14-1               DEF986
O14-1               AA1234
O14-2               BB123
O14-2               ADBR12

Output desired
Occ                       Date                  OccTitle           Related_Reports
O14-1                   25-01-2014        Title 1            ABC123,DEF986,AA1234
O14-2                   27-01-2014        Title 2            BB123,ADBR12
O13-25                15-11-2013         Title 3           '' (zero length string)


Thanks

Kelvin
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40225605
As long as the concatenated list of reports is less than 4000 characters try listagg:

select o.occ,
	to_char(o.somedate,'DD-MM-YYYY') mydate,
	o.occtitle,
	listagg(q.rpt_num,',') within group(order by rpt_num) related_reports
from tableo o
	left outer join oq q on o.occ=q.occ
group by o.occ,
	to_char(o.somedate,'DD-MM-YYYY'),
	o.occtitle
/

Open in new window


If it can exceed 4000 characters I would look at the XML trick or a customized function like tbl2str posted here:
http://www.experts-exchange.com/Database/Oracle/Q_26241385.html#a32934445

I'll work on the XML version and post back shortly.
0
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!

 
LVL 22

Author Comment

by:Kelvin Sparks
ID: 40225617
Thanks

t is very unlikely to ever come near 4000 char
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40225618
The XML version:
SELECT o.occ,
	to_char(o.somedate,'DD-MM-YYYY') mydate,
	o.occtitle,
       rtrim(
           extract(xmlagg(xmlelement("s", q.rpt_num || ',') order by rpt_num), '/s/text()').getclobval(),
           ','
       )
           related_reports
from tableo o
	left outer join oq q on o.occ=q.occ
group by o.occ,
	to_char(o.somedate,'DD-MM-YYYY'),
	o.occtitle
/

Open in new window

0
 
LVL 22

Author Closing Comment

by:Kelvin Sparks
ID: 40226115
By adapting this slightly, I was able to reduce query overhead by 90%. SQL Monitoring tool has now identified a similar expression that I can work on. As always, thanks for your prompt assistance.

Kelvin
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

688 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