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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 need a select with count query

Author  Topic 

mytestappn
Starting Member

1 Post

Posted - 2010-10-26 : 03:29:30
Hi,

I have table in the binary tree format:

Userid Parentid

004 0
006 004
008 004
010 006
015 006
025 008
030 008
040 010
045 010
048 015
051 015
054 025
060 025
075 030
078 030
004 040
084 040

Now, my issue is how to count the number of nodes of the subtree for each node.

for example,

if i take the node 006,the number of nodes of the subtree under 006 is 8.ie;006 is a Parentid for 2 Userid's:010 and 015.
Now,the count is 2.

Again, 010 and 015 are Parentid's for 4 Userid's:040,045,048,051.
Now the count has been incremented to 6.This process continues..

And the same goes for each node of the binary tree

Its very urgent..please help me

Thanks

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-10-26 : 04:00:24
Same question again? Lumbago..?



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2010-10-26 : 12:51:56
Did you ever bother to read the article I posted the first time this was posted? Go back and do that now:

http://www.simple-talk.com/sql/t-sql-programming/binary-trees-in-sql/

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-10-27 : 14:12:32
you should look at CTEs in books online

http://msdn.microsoft.com/en-us/library/ms186243.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -