﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / Development  / Counting all childs and sub-childs of a node in tree / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 16:45:30 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Counting all childs and sub-childs of a node in tree</title><link>http://www.sqlservercentral.com/Forums/Topic782737-145-1.aspx</link><description>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</description><pubDate>Tue, 12 Feb 2013 10:02:12 GMT</pubDate><dc:creator>anilkumar04_b</dc:creator></item><item><title>RE: Counting all childs and sub-childs of a node in tree</title><link>http://www.sqlservercentral.com/Forums/Topic782737-145-1.aspx</link><description>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.:-)</description><pubDate>Tue, 08 Sep 2009 02:04:40 GMT</pubDate><dc:creator>girish.envision</dc:creator></item><item><title>RE: Counting all childs and sub-childs of a node in tree</title><link>http://www.sqlservercentral.com/Forums/Topic782737-145-1.aspx</link><description>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[code]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[/code]</description><pubDate>Fri, 04 Sep 2009 02:13:58 GMT</pubDate><dc:creator>Dave Ballantyne</dc:creator></item><item><title>RE: Counting all childs and sub-childs of a node in tree</title><link>http://www.sqlservercentral.com/Forums/Topic782737-145-1.aspx</link><description>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.</description><pubDate>Fri, 04 Sep 2009 01:52:51 GMT</pubDate><dc:creator>girish.envision</dc:creator></item><item><title>RE: Counting all childs and sub-childs of a node in tree</title><link>http://www.sqlservercentral.com/Forums/Topic782737-145-1.aspx</link><description>Recursive CTE's are the way to go....Try this link [url]http://www.sqlservercentral.com/articles/Development/recursivequeriesinsqlserver2005/1760/[/url]</description><pubDate>Fri, 04 Sep 2009 01:21:23 GMT</pubDate><dc:creator>Dave Ballantyne</dc:creator></item><item><title>Counting all childs and sub-childs of a node in tree</title><link>http://www.sqlservercentral.com/Forums/Topic782737-145-1.aspx</link><description>Hi friends, I have one problem. I have one tree which I have saved in sql server table like[code]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[/code]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? [b]Please remember tree may have N number of nodes[/b], I have just used 12 here. We don't know total number nodes in the tree.[Sorry for the language-related mistakes].</description><pubDate>Fri, 04 Sep 2009 00:17:33 GMT</pubDate><dc:creator>girish.envision</dc:creator></item></channel></rss>