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 |
dlusignan
Starting Member
3 Posts |
Posted - 2009-10-26 : 11:08:21
|
Hi!Is this possible to have multiple trees in the same table like this:If yes, my next question will rely on Transact-SQL.CategoryID ParentID Name Lft Rgt----------- ----------- ------------------------- ----------- ----1 NULL Actualité 1 162 NULL Culture 1 1831 2 Arts de la scène 2 333 1 Communautaire 2 334 2 Divers 4 535 2 Babillard culturel 6 738 2 Expositions 8 940 2 Littérature 10 1141 2 Loisirs culturels 12 1342 1 Politique municipale 4 549 1 Environnement 6 750 1 Politique (autres) 8 951 2 Festivals 14 1552 2 Histoire et patrimoine 16 1756 1 Santé 10 1157 1 Éducation 12 1359 1 Faits divers 14 15 Daniel |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-10-26 : 11:21:23
|
Yes. The ID/parentID construct is one way to model a hierarchy and of course multiple 'trees' can be represented in the same table (as you have demonstrated). But I'm not sure what you're [Lft] and [Rgt] columns signify. Perhaps I don't get the question...Be One with the OptimizerTG |
 |
|
dlusignan
Starting Member
3 Posts |
Posted - 2009-10-26 : 11:41:15
|
Hi! Thanks for the answer. The use of Lft and Rgt permits you to define an order more easily for optimising the queries e.g.:SELECT ca.CategoryID, ca.ParentID, ca.[Name], ca.Lft, ca.RgtFROM printedproducts.Category caWHERE Lft = (Rgt - 1)AND ca.ParentID = 1 who give this result:CategoryID ParentID Name Lft Rgt33 1 Communautaire 2 342 1 Politique municipale 4 549 1 Environnement 6 750 1 Politique (autres) 8 956 1 Santé 10 1157 1 Éducation 12 1359 1 Faits divers 14 15 as you can see, it's an easy and fast way of accessing all the children in a hierarchy. In a production environment, with large amount of data, you can add an index on both Lft and Rgt.Daniel |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-10-26 : 12:59:15
|
So your parentID is really the Root ID, right? Can you express multiple nesting levels (like below)? With just ID/ParentID you can support a full hierachy with unlimited roots and unlimited sub-trees. Here is an example using a Recursive CTE:set nocount ondeclare @h table (id int, pid int)declare @n table (id int, name varchar(10))------------------------sample date--namesinsert @nselect 1, 'name1' union allselect 2, 'name2' union allselect 3, 'name3' union allselect 4, 'name4' union allselect 5, 'name5' union allselect 6, 'name6' union allselect 7, 'name7'--Hierachyinsert @hselect 1,null union allselect 2,1 union allselect 3,1 union allselect 4,2 union allselect 5,4 union allselect 6,null union allselect 7,6 --add a circular reference to make sure our recursive cte deals with itunion all select 1,5 ----------------------;with cte as( --Anchor member (no reference to cte) select h.id ,h.pid ,m.name ,0 as lev ,convert(varchar(50), right('00' + convert(varchar,h.id),3)) as sort from @n m join @h h on h.id = m.id where h.pid is null union all --recursion member because statement references cte select h.id ,h.pid ,m.name ,lev+1 -- datatyps must match (anchor and recusion member columns) ,convert(varchar(50), sort + '.' + right('00' + convert(varchar,h.id),3)) from @n m join @h h on h.id = m.id join cte y on y.id = h.pid --avoid a circular reference where patindex('%' + right('00' + convert(varchar,h.id),3) + '%', sort) = 0)select replicate(space(2), lev) + name tree --,*from cteorder by sort--option (maxrecursion 10)OUTPUT:tree------------name1 name2 name4 name5 name3name6 name7 Be One with the OptimizerTG |
 |
|
dlusignan
Starting Member
3 Posts |
Posted - 2009-10-26 : 14:07:44
|
Hi! Yes you can.Unfortunately, I can't figured out how to insert an image but, anyway, look at this snippet and try it in your Management Studio. You will figurate in the result, that it comes out with a boss employees relationship. Look at now, the way the columns Lft and Rgt are organized. You will easily figured out that everyone belongs to Albert but also, Donna, Eddie and Fred belong to Chuck.I recommanded you this book: Trees and Hierarchies in SQL For Smarties. It's all about the subject.declare @OrgChart TABLE ( Member CHAR(10), Lft int, Rgt int ) insert @OrgChartselect 'Albert', 1, 12 union allselect 'Bert', 2, 3 union allselect 'Chuck', 4, 11 union allselect 'Donna', 5, 6 union allselect 'Eddie', 7, 8 union allselect 'Fred', 9, 10SELECT Mgrs.member AS boss, Workers.member AS workerFROM @Orgchart AS Mgrs, @Orgchart AS WorkersWHERE Workers.lft > Mgrs.lftAND Workers.lft < Mgrs.rgt Daniel |
 |
|
|
|
|
|
|