| | |
Complex Query
Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
Greeting
When it comes to SQL I'm a novice and I think what I want to do is WAY outside my expertise. I have a file management system called ProjectWise. Everything to create the folder path is located in a single table. You start with the lowest folder then you need to find its parent. Once you've found the that parent you need to finds that folders parent and so on until you've created the entire path. I've attched an image file that shows this much better. I have no idea how to build this path with everything being in a single table. I don't mind reading I just need to be pointed in the right direction. Thank you for any help.
When it comes to SQL I'm a novice and I think what I want to do is WAY outside my expertise. I have a file management system called ProjectWise. Everything to create the folder path is located in a single table. You start with the lowest folder then you need to find its parent. Once you've found the that parent you need to finds that folders parent and so on until you've created the entire path. I've attched an image file that shows this much better. I have no idea how to build this path with everything being in a single table. I don't mind reading I just need to be pointed in the right direction. Thank you for any help.
I was thinking you could do it by joining the table to itself. But then you have the problem of not knowing if the page you're looking up is in the 3rd or 4th level of 'childhood'.
Anyway, this is the article on joining tables and this is the article on joining a table to itself.
If you decide to work with joins, you'll end up with a query similar to this:
You'll get a table that looks like this:
FILE | PARENT1 | PARENT2
--------------------------------------------------------------------
corridor_data | bia_walden_point | alaska
*EDIT #2*
This query will work only if there are 2 parent folders... I don't know how to dynamically change the query depending on the level of the file... sorry.
Anyway, this is the article on joining tables and this is the article on joining a table to itself.
If you decide to work with joins, you'll end up with a query similar to this:
MS SQL Syntax (Toggle Plain Text)
SELECT t1.filename AS file, t2.filename AS parent1, t3.filename AS parent2 FROM files AS t1 INNER JOIN files AS t2 ON t2.fileID = t1.parentID INNER JOIN files AS t3 ON t3.fileID = t2.parentID WHERE a1.fileID = 'the file you are looking for';
You'll get a table that looks like this:
FILE | PARENT1 | PARENT2
--------------------------------------------------------------------
corridor_data | bia_walden_point | alaska
*EDIT #2*
This query will work only if there are 2 parent folders... I don't know how to dynamically change the query depending on the level of the file... sorry.
Last edited by kanaku; Dec 29th, 2008 at 4:38 pm. Reason: Forgot to add the where clause...
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
I've thought about that. The biggest problem with it is that some of the path could be 8 to 10 parents deep. There is no way of knowing. I thought about a loop and just loop until o_parentno = 0 but I can't figure out how to read the o_projectno and then populate o_parentno with the value that came from o_projectno and loop through it again. Let alone display the folder name for each o_parentno.
It's really the most hard question I've met! loool I don't use to write SQL frequently, I just did the half of task to you, is to get parent ID, please care and ask if you didn't understand anything in my script, I work more than 2 hours in 

sql Syntax (Toggle Plain Text)
CREATE proc GetParent @Child INT, @Parent INT output AS SELECT @Parent = o_parentno FROM phillip WHERE o_projectno = @Child CREATE proc GetAllParent @ChildID INT AS if @ChildID != 0 BEGIN DECLARE @CurrentParent INT while @ChildID != 0 BEGIN exec GetParent @ChildID, @Parent = @CurrentParent output SELECT @CurrentParent SET @ChildID = @CurrentParent if @ChildID <= 0 break ELSE continue END END
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
There is something called 'case expressions' in MS SQL queries...
This article is a bit outdated but it still makes a lot of sense. Jump to the "A more complicated example" section to see how he does it.
This article is a bit outdated but it still makes a lot of sense. Jump to the "A more complicated example" section to see how he does it.
Last edited by kanaku; Dec 29th, 2008 at 6:54 pm.
What shall you to do is to call GetAllParents and give it the ChildID you need to get all its parents
sql Syntax (Toggle Plain Text)
GetAllParent 27937
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Welcome, Phillip please mark it as solved if it be.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
![]() |
Similar Threads
- Complex query returning one row from multiple rows (MySQL)
- Complex query help (MS SQL)
- Complex Query with PHP (PHP)
- Complex Query Help (MS SQL)
Other Threads in the MS SQL Forum
- Previous Thread: Notify a new row entry
- Next Thread: Debugger for PL/SQL in sql server
| Thread Tools | Search this Thread |






