Solved

folder path complex loop query

Posted on 2014-03-11
13
299 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cassandra Select Query 1 56
SQL Error in WHERE Clause 5 42
How to free up undo space? 3 29
Process mapping 5 29
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

867 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

20 Experts available now in Live!

Get 1:1 Help Now