Solved

folder path complex loop query

Posted on 2014-03-11
13
301 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Trying to get a Linked Server to Oracle DB working 21 60
dbms_crypto.decrypt   errors out 6 31
Run SQL Server Proc from Access 11 31
VB.net and sql server 4 36
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Via a live example, show how to take different types of Oracle backups using RMAN.

777 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