Access Query: Sub-Assembly Roll-up

时间:2019-03-06 11:36:56

标签: sql ms-access select

I have a database that contains Bill of Materials for products but some of these items are sub-assemblies. e.g.

Normal Product (Not relevant, just an example):

MyNormalProduct:
      Item Name             Quantity     Cost
    - Item 1                1            $50
    - Item 2                1            $25

Product with Sub-Assembly:

MySubAssemblyProduct:
      Item Name              Quantity     Cost
    - Item 1                 1            $100
    - Sub-Assembly Item      1
        - Sub Part 1         1            $100     
        - Sub Part 2         1            $100

What I need to do is have a query that rolls up the cost of all the Sub Items onto one line on a report. So the output will look like this:

Item Name              Component          Quantity    Cost
MySubAssemblyProduct   Item 1                 1       $100
MySubAssemblyProduct   Sub-Assembly Item      1       $200

To help with this I have three tables tblProducts, tblLink and tblItems.

tblProducts:

This contains the header information:

tblProducts

tblLink:

A simple table that helps me link the products, if there is a value in ParentItem then the link is to a Sub-Assembly. In the image, the red items are top level, the orange items are Sub-assembly. The relevant quantities are also stored in here too. You will see that MaterialProcessSKU 7 and 8 belong to MaterialProcessSKU 6.

enter image description here

tblItems:

This is the table that contains all of the Items, with relevant costs and other information.

tblItems

I can get a list of the Sub-Assemblies using the following SQL:

SELECT tblItems.Type, tblLink.SKU, tblItems.ItemNo, tblItems.[Item Name], tblItems.[Buy Quantity], tblLink.Quantity, tblItems.[Unit of Measure], tblItems.Waste, 0 AS [Laser Mins], 0 AS [Labour Mins], tblItems.Cost AS [$ Rate], ([tblLink].[Quantity]*[tblItems].[Cost])+(([tblLink].[Quantity]*[tblItems].[Cost])*[tblItems].[waste]) AS [$ Cost], tblLink.ParentItem

FROM (tblLink INNER JOIN tblProducts ON tblLink.SKU = tblProducts.SKU) 
INNER JOIN tblItems ON tblLink.MaterialProcessSKU = tblItems.ItemNo
WHERE (((tblItems.Type)="Sub-Assembly") AND ((tblLink.ParentItem) Is Null));

and I get this output, but obviously the $ Cost is blank:

Sub-Assembly Output

I can also get the total of the Sub-Assembly using this SQL - you will see I am manually defining the ParentItem as 6:

SELECT Sum([Cost]*[Quantity]) AS TotalCost
FROM tblProducts, tblLink INNER JOIN tblItems ON tblLink.MaterialProcessSKU = tblItems.ItemNo
GROUP BY tblLink.SKU, tblLink.ParentItem, tblLink.Quantity, tblItems.Cost
HAVING (((tblLink.ParentItem)=6));

This is the output:

Total Cost

I need to merge the two SQL Statements so I get the TotalCost from the above SQL Query in the $ Cost in the first SQL Query.

Please can someone point me in the right direction?

Thanks Chris

* UPDATE *

I've tried to get started with the following:

SELECT tblItems.ItemNo, c.TotalCost as NewCost 

FROM tblItems

 inner join (select tblItems.ParentItem from tblLink group by ParentItem) b on tblItems.ItemNo = b.ParentItem

 inner join (select tblItems.ItemNo,sum(Cost) as TotalCost from tblItems group by tblItems.ItemNo) c on tblItems.ItemNo = b.ParentItem

But I'm getting a Syntax Error (Missing Operator).

Can anyone help please?

0 个答案:

没有答案