Solved

folder path complex loop query

Posted on 2014-03-11
13
308 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
[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
  • 7
  • 6
13 Comments
 
LVL 74

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 74

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
Get Database Help Now w/ Support & Database Audit

Keeping your database environment tuned, optimized and high-performance is key to achieving business goals. If your database goes down, so does your business. Percona experts have a long history of helping enterprises ensure their databases are running smoothly.

 
LVL 5

Author Comment

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

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
 
LVL 74

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 74

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 74

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 74

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

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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 video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

739 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