Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
graff101
Starting Member
4 Posts |
Posted - 2009-04-02 : 11:30:30
|
Hi I have read many posts and articles on recursive/tree queeries and I am struggling to order my reults. I have a table with a recursive join (sectionID & parentId) that I would like to query in order to build a tree structure of links. Table SECTIONsectionID...title........displayOrder.....parentID1.............About......1...................NULL2.............Vessels....2...................NULL3.............Outro......3...................NULL4.............Tanker.....1...................25.............Offshore...2...................2QueryWITH CTE1 AS ( SELECT distinct sectionID,parentID, title,displayOrder, 1 AS Level FROM SECTION where parentID is null UNION ALL SELECT E.sectionID, E.parentID,E.title, E.displayOrder, C.Level + 1 FROM SECTION E INNER JOIN CTE1 C ON E.parentID = C.sectionID WHERE E.parentID is not NULL)SELECT title FROM CTE1 ORDER BY Level, displayOrderThe result returned looks like this...AboutVesselsOutro-Tanker-OffshoreBut I would like my query result to look likeAboutVessels-Tanker-OffshoreOutro I'm not very expereinced in SQL like this and although I've managed to adapt certain tutorials to get this far my understanding of what actually is happening is very limited so any help would be greatly appreciated. |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2009-04-05 : 21:25:42
|
try WITH CTE1 AS (SELECT distinct sectionID,parentID, title,displayOrder, 1 AS Level FROM SECTIONwhere parentID is nullUNION ALLSELECT E.sectionID, E.parentID,E.title, E.displayOrder, C.Level + 1FROM SECTION EINNER JOIN CTE1 CON E.parentID = C.sectionID WHERE E.parentID is not NULL)SELECT title FROM CTE1 ORDER BY Level, coalesce(ParentID,displayorder),displayOrder Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
graff101
Starting Member
4 Posts |
Posted - 2009-04-14 : 10:42:16
|
| Hi Thank yu for your reply. Unfortunately this only works if the sectionID's are in a sequential order. My result needs to work by listing all parents and children in the order defined by the displayOrder column.I have found this query but it doesn't work for grandchildren.WITH DirectReports (parentID, sectionID, title, alias, displayOrder, Level, Path)AS( SELECT parentID, sectionID, title, alias, displayOrder, 0 AS Level, CAST(sectionID AS VARCHAR(MAX)) AS Path FROM SECTION WHERE (parentID IS NULL) AND (presentationID = @presentationID) UNION ALL SELECT e.parentID, e.sectionID, e.title, e.alias, e.displayOrder, Level + 1, Path + '/'+ CAST(Level + 1 AS VARCHAR(MAX)) FROM SECTION AS e JOIN DirectReports AS d ON e.parentID = d.sectionID)SELECT sectionID , parentID ,title, alias ,Level,Path ,displayOrderFROM DirectReportsORDER BY Path, displayOrder ascAnyone got any ideas how to format these results so that I can then produce this dynamic re-structuring for my site? |
 |
|
|
|
|
|
|
|