Complex Query

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 3
Reputation: Phillip_Hess is an unknown quantity at this point 
Solved Threads: 0
Phillip_Hess Phillip_Hess is offline Offline
Newbie Poster

Complex Query

 
0
  #1
Dec 29th, 2008
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.
Attached Thumbnails
Folder_Paths.png  
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 276
Reputation: kanaku is on a distinguished road 
Solved Threads: 15
kanaku's Avatar
kanaku kanaku is offline Offline
Posting Whiz in Training

Re: Complex Query

 
0
  #2
Dec 29th, 2008
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:
  1. SELECT
  2. t1.filename AS file,
  3. t2.filename AS parent1,
  4. t3.filename AS parent2 FROM files AS t1
  5. INNER JOIN
  6. files AS t2
  7. ON t2.fileID = t1.parentID
  8. INNER JOIN
  9. files AS t3
  10. ON t3.fileID = t2.parentID
  11. WHERE
  12. 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...
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: Phillip_Hess is an unknown quantity at this point 
Solved Threads: 0
Phillip_Hess Phillip_Hess is offline Offline
Newbie Poster

Re: Complex Query

 
0
  #3
Dec 29th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Complex Query

 
0
  #4
Dec 29th, 2008
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

  1. CREATE proc GetParent
  2. @Child INT,
  3. @Parent INT output
  4. AS
  5. SELECT @Parent = o_parentno
  6. FROM phillip
  7. WHERE o_projectno = @Child
  8.  
  9. CREATE proc GetAllParent
  10. @ChildID INT
  11. AS
  12. if @ChildID != 0
  13. BEGIN
  14. DECLARE @CurrentParent INT
  15. while @ChildID != 0
  16. BEGIN
  17. exec GetParent @ChildID, @Parent = @CurrentParent output
  18. SELECT @CurrentParent
  19. SET @ChildID = @CurrentParent
  20. if @ChildID <= 0
  21. break
  22. ELSE
  23. continue
  24. END
  25. END
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 276
Reputation: kanaku is on a distinguished road 
Solved Threads: 15
kanaku's Avatar
kanaku kanaku is offline Offline
Posting Whiz in Training

Re: Complex Query

 
0
  #5
Dec 29th, 2008
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.
Last edited by kanaku; Dec 29th, 2008 at 6:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Complex Query

 
0
  #6
Dec 29th, 2008
What shall you to do is to call GetAllParents and give it the ChildID you need to get all its parents

  1. GetAllParent 27937
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: Phillip_Hess is an unknown quantity at this point 
Solved Threads: 0
Phillip_Hess Phillip_Hess is offline Offline
Newbie Poster

Re: Complex Query

 
0
  #7
Dec 30th, 2008
WOW This is great... I can work with this. I've put the syntax into SQL Query Analyzer and ran it. It stops on Line 9 "CREATE PROC GetAllParent". But I can work with this. With the article you provided and a little more digging maybe I can figure this out. Thanks again for all your help.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Complex Query

 
0
  #8
Dec 30th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC