?
Solved

sys_connect_by_path subquery

Posted on 2014-07-28
6
Medium Priority
?
445 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 2000 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

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…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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.
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
Suggested Courses

800 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