Solved

sys_connect_by_path subquery

Posted on 2014-07-28
6
391 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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
PL SQL Search Across Columns 4 52
Oracle Verification of DataPump Export and Import 17 60
Input a SQl, output tables amd columns used in the SQL 19 55
Fill Null values 5 28
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

839 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