Solved

sys_connect_by_path subquery

Posted on 2014-07-28
6
382 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
  • 3
  • 3
6 Comments
 
LVL 76

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 76

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
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 22

Author Comment

by:Kelvin Sparks
ID: 40225617
Thanks

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

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

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

770 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