Here is an assembly tree that I want to search using a recursive T-SQL
Query (presumably CTE
) with the expected results below. I want to know the total amount per assembly given any part.
Meaning if I search for 'Rivet', I want to know the total count at each level within the assembly, not just the direct children count.
Assembly (id:1)
|
|-Rivet
|-Rivet
|-SubAssembly (id:2)
| |
| |-Rivet
| |-Bolt
| |-Bolt
| |-SubSubAssembly (id:3)
| |
| |-Rivet
| |-Rivet
|
|-SubAssembly (id:4)
|-Rivet
|-Bolt
DESIRED Results
-------
ID, Count
1 , 6
2 , 3
3 , 2
4 , 1
Currently, I can get the direct parents, but want to know how to extend my CTE to allow me to roll this information upward.
With DirectParents AS(
--initialization
Select InstanceID, ParentID
From Instances i
Where i.Part = 'Rivet'
UNION ALL
--recursive execution
Select i.InstanceID, i.ParentID
From PartInstances i INNER JOIN DirectParents p
on i.ParentID = p.InstanceID
)
select ParentID, Count(instanceid) as Totals
from DirectParents
group by InstanceID, ParentID
Results
-------
ID, Count
1 , 2
2 , 2
3 , 2
4 , 1
Creation script
CREATE TABLE [dbo].[Instances] (
[InstanceID] NVARCHAR (50) NOT NULL,
[Part] NVARCHAR (50) NOT NULL,
[ParentID] NVARCHAR (50) NOT NULL, );
INSERT INTO Instances
Values
(1, 'Assembly', 0),
(50, 'Rivet', 1),
(50, 'Rivet', 1),
(2, 'SubAssembly', 1),
(50, 'Rivet', 2),
(51, 'Bolt', 2),
(51, 'Bolt', 2),
(3, 'SubSubAssembly', 2),
(50, 'Rivet', 3),
(50, 'Rivet', 3),
(4, 'SubAssembly2', 1),
(50, 'Rivet', 4),
(51, 'Bolt', 4)