Question : SQL Query Problem

I have been asked to find out how much material we have used in one month by interrogating the SQL database.

I can return all the top level parts by using the following code.
1:
2:
3:
4:
5:
6:
7:
SELECT     InvMovements.StockCode AS StockCode, SUM(InvMovements.TrnQty) AS QTY
FROM         InvMovements INNER JOIN
                      InvMaster ON InvMovements.StockCode = InvMaster.StockCode
WHERE     (InvMovements.EntryDate BETWEEN CONVERT(DATETIME, '2010-05-31 00:00:00', 102) AND CONVERT(DATETIME, '2010-06-27 00:00:00', 102)) AND 
                      (InvMovements.TrnType = 'R') AND (InvMaster.ProductClass IN ('PDM', 'PRM', 'TM', 'PM'))
GROUP BY InvMovements.StockCode
ORDER BY InvMovements.StockCode


This returns the partnumber (stockcode) and the QTY manufactured for the month.
I then need to access the bom table to return the materials used for the parts.
The bom table has the following structure:
ParentPart
Component
QtyPer

The problem is that for any given ParentPart the Component might also be made up of several parts.
e.g.  
ParentPart xxx is made of Component YYYY and uses QtyPer 12
ParentPart xxx is made of Component ZZZZ and uses QtyPer 2
ParentPart xxx is made of Component WWW and uses QtyPer 3

The problem I have is Component ZZZZ is made up of
ParentPart ZZZZ is made of Component aaaa and uses QtyPer 4
ParentPart ZZZZ is made of Component bbbb and uses QtyPer 7

For the example above I would need to return
ParentPart xxx  
uses
Part YYYY Qty 12
Part aaaa Qty 8
Part bbbb Qty 14
Part WWWW Qty 3

and then multiply the individual Part QTY by the QtyPer returned in the original SQL.

Answer : SQL Query Problem

select X.StockCode [topLevelPart], X.Qty [topQty],
      Coalesce(E.Component,D.Component,C.Component,B.Component,A.Component) [lowerLevelPart],
      ISNULL(E.QtyPer,1)*ISNULL(D.QtyPer,1)*ISNULL(C.QtyPer,1)*ISNULL(B.QtyPer,1)*ISNULL(A.QtyPer,1)*X.Qty [Qty]
from (
      SELECT InvMovements.StockCode AS StockCode, SUM(InvMovements.TrnQty) AS QTY
      FROM   InvMovements
      INNER JOIN InvMaster ON InvMovements.StockCode = InvMaster.StockCode
      WHERE (InvMovements.EntryDate BETWEEN CONVERT(DATETIME, '2010-05-31 00:00:00', 102)
                                                        AND CONVERT(DATETIME, '2010-06-27 00:00:00', 102))
        AND (InvMovements.TrnType = 'R') AND (InvMaster.ProductClass IN ('PDM', 'PRM', 'TM', 'PM'))
      GROUP BY InvMovements.StockCode
) X
inner join BOM A on A.ParentPart = X.StockCode
left join BOM B on B.ParentPart = A.Component
left join BOM C on C.ParentPart = B.Component
left join BOM D on D.ParentPart = C.Component
left join BOM E on E.ParentPart = D.Component
ORDER BY X.StockCode
Random Solutions  
 
programming4us programming4us