The last one Orasis handled was this one. As real as it gets... Complicated enough you think? Never doubt unless you try...
With BOM_cte(ProductAssemblyID, ComponentID, ComponentDesc, PerAssemblyQty,
StandardCost, ListPrice, BOMLevel, RecursionLevel) As (Select
b.ProductAssemblyID, b.ComponentID, p.Name, b.PerAssemblyQty,
p.StandardCost, p.ListPrice, b.BOMLevel, 0
From Production.BillOfMaterials b Inner Join
Production.Product p On b.ComponentID = p.ProductID
Where b.ProductAssemblyID = @StartProductID And @CheckDate >= b.StartDate
And @CheckDate <= ISNULL(b.EndDate, @CheckDate)
Union All
Select b.ProductAssemblyID, b.ComponentID, p.Name, b.PerAssemblyQty,
p.StandardCost, p.ListPrice, b.BOMLevel, RecursionLevel + 1
From BOM_cte cte Inner Join
Production.BillOfMaterials b On b.ProductAssemblyID = cte.ComponentID
Inner Join
Production.Product p On b.ComponentID = p.ProductID
Where @CheckDate >= b.StartDate And @CheckDate <= ISNULL(b.EndDate,
@CheckDate))
Select b.ProductAssemblyID, b.ComponentID, b.ComponentDesc,
Sum(b.PerAssemblyQty) As TotalQuantity, b.StandardCost, b.ListPrice,
b.BOMLevel, b.RecursionLevel
From BOM_cte b
Group By b.ProductAssemblyID, b.ComponentID, b.ComponentDesc, b.StandardCost,
b.ListPrice, b.BOMLevel, b.RecursionLevel
Order By b.BOMLevel, b.ProductAssemblyID, b.ComponentID