Solved

folder path complex loop query

Posted on 2014-03-11
13
296 Views
Last Modified: 2014-03-13
- This is complicated query, we have 3 tables document, folder, path
- Table 1 (i.e. document) can be easily joined by table 2 based on ID and LID and we can get FOLDID
- We take that FOLDID and use that to construct the path where that document is located.
- TABLE 3 (Path) Do a split or substring on FOLDID and get the FLDKEY in above example 56777...Based on FLDKEY save the FLDSTRING as the "current folder" document is located. Loop through using FLDKEY2 in path table and find the second current folder again ... loop through again until FLDKEY2 = 'ROOT'
Build-Folder-Path---Sheet1.csv
0
Comment
Question by:CalmSoul
  • 7
  • 6
13 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 39921267
please post expected output that  corresponds to your sample input.

also your sample input seems incomplete given that we are supposed to look for "ROOT" elements but no such element exists in the sample.
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 39921315
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39921430
SELECT p.fldstring,
       p2.fldstring,
       p3.fldstring,
       d.doc_name
  FROM document d
       INNER JOIN folder f ON d.id = f.lid
       INNER JOIN PATH p ON f.foldid = p.fldtype || '-' || p.fldkey
       LEFT JOIN PATH p2 ON p2.fldkey = p.fldkey2
       LEFT JOIN PATH p3 ON p3.fldkey = p2.fldkey2;



Searching for ROOT explicitly doesn't seem to be necessary, once you reach the root, the chain will stop anyway because there won't be anything to connect fldkey and fldkey2
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 39921554
Sorry one change FOLDID = {ABCD}56777 instead of ABCD-56777
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39921628
change

       INNER JOIN PATH p ON f.foldid = p.fldtype || '-' || p.fldkey

to

       INNER JOIN PATH p ON f.foldid = '{' || p.fldtype || '}' || p.fldkey
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 39922280
What happens for instance there is no "FOLDID" for DOC_NAME?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 73

Expert Comment

by:sdstuber
ID: 39922303
If there's no foldid then the join conditions won't pick up those records.
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 39922327
Can we pick up those records?
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39922429
post sample data and expected results for that condition
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 39922440
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39922466
your sample data had the ABCD-56777 format instead of {ABCD}56777

so, if you'll always have at least document and folder then you'll INNER join those and OUTER join the path


SELECT p.fldstring folder_level_1,
       p2.fldstring folder_level_2,
       p3.fldstring folder_level3,
       d.doc_name
  FROM document d
       INNER JOIN folder f ON d.id = f.lid
       LEFT JOIN PATH p ON f.foldid = p.fldtype || '-' || p.fldkey
       LEFT JOIN PATH p2 ON p2.fldkey = p.fldkey2
       LEFT JOIN PATH p3 ON p3.fldkey = p2.fldkey2;


If you want the {} format then change

LEFT JOIN PATH p ON f.foldid = p.fldtype || '-' || p.fldkey

to

LEFT JOIN PATH p ON f.foldid = '{' || p.fldtype || '}' || p.fldkey
0
 
LVL 5

Author Comment

by:CalmSoul
ID: 39925005
Hello sdstuber:

Left join is not doing the trick... Anything else I can try?

thanks
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39925012
It worked for me with the data you provided.

Can you post an example where it doesn't work?
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
query help 18 51
How to convert sql code to access query 8 55
Schema creation in Oracle12c 6 24
sql query Help 12 29
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.​
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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 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.

758 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

22 Experts available now in Live!

Get 1:1 Help Now