## Counting all childs and sub-childs of a node in tree

 Author Message girish.envision Forum Newbie Group: General Forum Members Points: 3 Visits: 20 Hi friends, I have one problem. I have one tree which I have saved in sql server table like`ID Node ParentID1 A 02 B 13 C 14 D 25 E 26 F 37 G 38 H 3 9 I 410 J 411 K 1012 L 11`ParentID stores ID of the parent of the node.Now suppose I have only one node given from the table, say "A", I have to count all its child nodes up to the leafs. In above table under node "A", there are in-total 11 child and sub-child nodes , if I have node "B" then there are total 6 child and sub-child nodes under it.How to achieve this in sql server (withought using loops) if we are provided with only one node from the tree and we have to count all the child and sub-child up to leafs? Please remember tree may have N number of nodes, I have just used 12 here. We don't know total number nodes in the tree.[Sorry for the language-related mistakes]. Attachments Hierarchy.JPG (13 views, 12.00 KB) Dave Ballantyne SSCommitted Group: General Forum Members Points: 1818 Visits: 8370 Recursive CTE's are the way to go....Try this link http://www.sqlservercentral.com/articles/Development/recursivequeriesinsqlserver2005/1760/ Clear Sky SQLMy Blog girish.envision Forum Newbie Group: General Forum Members Points: 3 Visits: 20 thanks for replying dave.....Actually I have used recursive queries with some different scenarios in hierarchies. But here the case is little different i have to count all nodes, for this i will have traverse from top to bottom. In case of the example i have given in my question, from A i will have to search child nodes, which are B and C. then from B i will have to get D and E and from C have to find F, G and H. then again i will have to look for the child nodes of these nodes also. I tried the recursive query, but I didn't find any fruitful result.But ok, I will try it again. Thanks once again for reply. Dave Ballantyne SSCommitted Group: General Forum Members Points: 1818 Visits: 8370 Try this, possibly not the most efficient, i suspect that a hierarchyId-esque based query may be faster over a large dataset , but should do`Create table #RecursTest(ID integer,Node varchar(10),ParentId integer)insert into #RecursTest(Id,Node,ParentID) values(1, 'A', 0)insert into #RecursTest(Id,Node,ParentID) values(2, 'B', 1)insert into #RecursTest(Id,Node,ParentID) values(3, 'C', 1)insert into #RecursTest(Id,Node,ParentID) values(4, 'D', 2)insert into #RecursTest(Id,Node,ParentID) values(5, 'E', 2)insert into #RecursTest(Id,Node,ParentID) values(6, 'F', 3)insert into #RecursTest(Id,Node,ParentID) values(7, 'G', 3)insert into #RecursTest(Id,Node,ParentID) values(8, 'H', 3)insert into #RecursTest(Id,Node,ParentID) values(9, 'I', 4)insert into #RecursTest(Id,Node,ParentID) values(10, 'J', 4)insert into #RecursTest(Id,Node,ParentID) values(11, 'K', 10)insert into #RecursTest(Id,Node,ParentID) values(12, 'L', 11)gowith cteRecurs(AncestorId,Id,Node,ParentId)as( Select Id,Id,Node,ParentId from #RecursTest union all Select cteRecurs.AncestorId,#RecursTest.Id,#RecursTest.Node,#RecursTest.ParentId from cteRecurs, #RecursTest where #RecursTest.ParentId = cteRecurs.Id)select AncestorId,count(*)-1 From cteRecursgroup by AncestorId` Clear Sky SQLMy Blog girish.envision Forum Newbie Group: General Forum Members Points: 3 Visits: 20 hey dave, sorry for replying late. i tested it with the same logic but in a different way. it worked. also i tried it using cursors too. that also worked. but as cursors are not that efficient, i kept this CTE logic.thanks for the help.:-) anilkumar04_b Forum Newbie Group: General Forum Members Points: 3 Visits: 9 Hi All,I need an urgent help to build some logic in Oracle DB.I have a table called TREE and in that we have data based on hierarchies.We have columns like DataID and ParentID and DataID column is unique one.And the data for DataID and ParentID will be this is like parent and child relation ship.I need to get the count of all DataID's and Sub-dataID's of ParentID.I need to get the count of this without using Start With connect by option.Is there a way to get this done...ThanksAnilkumar B kunalchatterjeeit Forum Newbie Group: General Forum Members Points: 1 Visits: 0 Thank u for the post. It helps me lot.