Recursive CTE is powerfull tool for bill of material calculations.
The query below calculates total parts, needed for top-level assemblies
declare @BOM table( id int ,parentId int ,qty float ) insert @BOM values ( 20,10,2) ,(25,10,3) ,(30,20,5) ,(30,25,7) ,(40,30,11) ;with cte as( select [parentId] as assmblId, id, parentId, qty, qty as totalQty from @BOM where [parentId] not in (select id from @BOM) union all select cte.assmblId, bom.id, bom.parentId, bom.qty, cte.totalQty * bom.qty from cte join @BOM bom on cte.id = bom.parentId ) select assmblId, id, sum(totalQty) from cte group by assmblId, idBut use it wisely as it is the simple syntax for which may turn to be huge amount of data processing.
Serg