Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
It was very well written solution for "
SQL Queries to Manage Hierarchical or Parent-child Relational Rows in SQL Server
"
at
[^]

I have table called DocuTree and it has few columns but main columns are PArentID, ChildID, Name, Date and i would like to get the counts for each Parent/Child records in a
Hierarchical way 
, If you can help me out. We have multiple
Hierarchic
so i wanted get the both the info.

Hierarchic Level counts

CORP	18

Under CORP out of 18, one is Sales
Sales Have 4 folder counts
under Sales 
		Counts
ITTemp		0
Agreements	1
Contracts	6
Procedures	10

If I take Contracts then
Sales-Contract_AG	Sales-Contract_DEF	Sales-Contract_EXP	Sales-Contract_IND	Sales-Contract_INT	Sales-Contract_LT
4			5			1			4			1			3

If i take as a example Sales-Contract_AG then it has another 4 folder like

Sales-Contract_AG-Assign	0
Sales-Contract_AG- IProg	1
Sales-Contract_AG -MP		5
Sales-Contract_AG -Term		2

 need all PArent and Child and Sub-child folders counts by name


What I have tried:

Counts Hierarchical way in sql query
Posted
Updated 31-Oct-19 16:53pm

For a Parent-Child self-referencing table, there are going to be different routines to be done:

Build the hierarchical structure
You already have the database table, you are going to need a recursive function to generate a dataset that reflects how that structure looks as a tree. There are many examples of sample code for this available, including this one with some management functions located here
SQL Queries to Manage Hierarchical or Parent-child Relational Rows in SQL Server[^]

Count the children for any particular parent
This is going to be a simple one; basically you need to do a count of children for each parent

Tips & Hints
When my web applications using parent-child structures are edited; I fire off a stored procedure that rebuilds the hierarchy and saves it into a new table so that the recursion does not need to be done at every call.
On the parent-cild table itself, there are fields that contain the counts for children as well as how many items are in that category.

Update
Sample Code for Counting the Children
Please note the square brackets around [Name]; this is because it is a reserved word. It is generally recommended not to use these when you are designing tables etc as it can cause problems if the square brackets are omitted
SQL
SELECT    p.[Name], Children = Count(c.ParentID)
FROM      DocuTree  p
LEFT JOIN DocuTree  c ON p.ChildID = c.PhildID
GROUP BY  p.[Name]
 
Share this answer
 
v2
Comments
pdsQ 5-Nov-19 10:18am    
Very Good, Really Appreciate it!

Thank you so much!
Hi,
Thanks for the providing link, it shows parent and child hierarchical but i am looking counts for each category and subcategory.
Example:
Hierarchic Level counts

CORP	18

Under CORP out of 18, one is Sales
Sales Have 4 folder counts
under Sales 
		Counts
ITTemp		0
Agreements	1
Contracts	6
Procedures	10

If I take Contracts then
Sales-Contract_AG	Sales-Contract_DEF	Sales-Contract_EXP	Sales-Contract_IND	Sales-Contract_INT	Sales-Contract_LT
4			5			1			4			1			3

If i take as a example Sales-Contract_AG then it has another 4 folder like

Sales-Contract_AG-Assign	0
Sales-Contract_AG- IProg	1
Sales-Contract_AG -MP		5
Sales-Contract_AG -Term		2

 need all PArent and Child and Sub-child folders counts by name
 
Share this answer
 
Comments
MadMyche 1-Nov-19 7:14am    
Please see the updated answer
Richard Deeming 1-Nov-19 13:34pm    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution and post a comment.

Do not post your comment as a "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900