SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 child-node counting in tree problem
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sigmas
Posting Yak Master

Belarus
172 Posts

Posted - 11/04/2013 :  13:25:11  Show Profile  Reply with Quote
hi,
see:

                        A
		      /  \ 
                     B    C
		     |
		     D

A 3
B 1
C 0
D 0


the query?

Edited by - sigmas on 11/04/2013 13:26:27

James K
Flowing Fount of Yak Knowledge

3326 Posts

Posted - 11/04/2013 :  13:35:40  Show Profile  Reply with Quote
How is the tree stored in your database? I am visualizing a recursive query that can do the counting, but how one would implement that depends on how the data is stored.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 11/04/2013 :  13:38:01  Show Profile  Reply with Quote
How is it implemented in table? Using Heirarchyid datatype or using a recursive CTE?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

sigmas
Posting Yak Master

Belarus
172 Posts

Posted - 11/04/2013 :  13:41:23  Show Profile  Reply with Quote
child-parent
A null
B A
C A
D B
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 11/04/2013 :  14:00:47  Show Profile  Reply with Quote
The code will be in lines of this

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=189388

with a slight tweak to COUNT the child values rather than concatenation
Make a try and revert if you face any issues

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

sigmas
Posting Yak Master

Belarus
172 Posts

Posted - 11/04/2013 :  14:22:16  Show Profile  Reply with Quote
Is this correct?

declare @emp table
(
EmployeeID int,
BossEmployeeID int
)
 
insert @emp
values
(1, NULL),
(2, 1),
(3, 1),
(4, 2);

With OrgHierarchy
AS
(
SELECT  counting = 0, *        
FROM @emp t
WHERE NOT EXISTS (SELECT 1
FROM @emp WHERE  [BossEmployeeID] = t.[EmployeeID])

UNION ALL

SELECT oh.counting + 1, t.*
FROM OrgHierarchy oh
INNER JOIN @emp t
ON t.[EmployeeID] = oh.[BossEmployeeID]
)

SELECT sum(counting) as counting,
employeeid
FROM OrgHierarchy
group by employeeid
ORDER BY [EmployeeID]
OPTION (MAXRECURSION 0)



Edited by - sigmas on 11/04/2013 14:23:03
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 11/05/2013 :  00:31:19  Show Profile  Reply with Quote
Why not test it yourself with the full data you've?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

sigmas
Posting Yak Master

Belarus
172 Posts

Posted - 11/05/2013 :  06:58:18  Show Profile  Reply with Quote
I just want to know that query logicality is correct or not.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 11/05/2013 :  08:06:23  Show Profile  Reply with Quote
Did you get your expected result out of the sample data you tried?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000