Question

This is my first question so forgive me if I am not clear enough.

I am tasked with getting the total cost of all components for an Assembly(Bill of Materials). Basically, I want to query the table that holds purchase order allocation for the components so I can get cost currently associated with the unposted assembly.

It gets tricky because a component of an assembly could be an assembly itself, and in that case I need to query another table that holds information on whether other assemblies are linked to this main one. (I have checks in place to make sure over allocation does not occur, but it's possible nothing has been allocated yet and that's OK) In this case query the purchase order allocation table for the components of THAT assembly number, and add those costs to the total for the parent assembly.

I'm using a CTE, first time, and not having much luck. Can anyone possibly help determine what I'm doing wrong here?

The anchor pulls in all the components, excluding sub-assemblies, and their unit costs and qty from PO for the main assembly I'm trying to determine cost for.

The recursive part should be pulling components, cost and qty for the assemblies that have been 'linked' by existing on the BM10200_AssemblyQtyDetail table. If the parent assembly is in the TRANNUM column, then the TRX_ID of that row is the linked assembly that is a sub-assembly component of the main assembly.

USE HT
GO
WITH BOMCost (Assembly, Component, PriceFromPO, Qty, BOMLevel)
AS
(
-- Anchor member definition
SELECT asl.TRX_ID, asl.ITEMNMBR, asl.UNITCOST, asl.SERLTQTY,
0 AS BOMLevel
FROM HT.DBO.BM10400 AS asl
WHERE asl.TRX_ID = 'ASM0002909'
UNION ALL
-- Recursive member definition
SELECT asl.TRX_ID, asl.ITEMNMBR, asl.UNITCOST, asl.SERLTQTY,
    BOMLevel + 1
FROM HT.DBO.BM10400 AS asl
INNER JOIN HT.DBO.BM10200_AssemblyQtyDetail AS bqd 
    ON asl.TRX_ID = bqd.TRANNUM
INNER JOIN BOMCost AS bc
    ON bqd.TRX_ID = bc.Assembly
)


-- Statement that executes the CTE
SELECT Assembly, Component, PriceFromPO, Qty, BOMLevel
FROM BOMCost

To reiterate, the problem here is this ONLY returns the components of the main assembly sans any sub-assembly related costs. SO just components of the top level. I have a record in the linking table linking an assembly for the sub-assembly of this BM to the main assembly, yet it does not pull the components for that assembly number. I think it may have something to do with the recursive sections joins. Any help is appreciated!

Here's the data scenario. ASM0002909 is an assembly with a component that is itself an assembly. ASM0002914 is being built simultaneously for ASM0002909 and itself has two components. I only want to get the cost of whats actually been received from PO so it does not matter if other components are necessary but have not been received yet. Here's what all should be summed (SERLTQTY * UNITCOST) To get the total cost for the main assembly.

Assembly Serial Lot Table

Yet here is the result I'm currently getting from my Query. It should be running the recursion step once and returning the ASM0002914 results. Results from my CTE

I've set up a sqlfiddle with the two main tables, data, and with the sql query I'm currently using which is still not picking up the subassemblies components and is returning a recursion limit exceeded error. http://sqlfiddle.com/#!3/bd1b98/6

Était-ce utile?

La solution

I may be missing something, but it appears your JOIN criteria precludes recursion:

INNER JOIN BOMCost AS bc ON bqd.TRX_ID = bc.Assembly

ASM0002909 doesn't equal ASM0002914, so no results from the bottom portion.

Update:

WITH BOMCost (Assembly, Component, PriceFromPO, Qty, BOMLevel)
AS
(
-- Anchor member definition
    SELECT asl.TRX_ID, asl.ITEMNMBR, asl.UNITCOST, asl.SERLTQTY,
    0 AS BOMLevel
    FROM DBO.BM10400 AS asl
    WHERE asl.TRX_ID = 'ASM0002909'
    UNION  ALL
-- Recursive member definition
    SELECT asl.TRX_ID, asl.ITEMNMBR, asl.UNITCOST, asl.SERLTQTY,
        BOMLevel + 1
    FROM DBO.BM10400 AS asl
    INNER JOIN DBO.BM10200_AssemblyQtyDetail AS bqd 
        ON asl.TRX_ID = bqd.TRX_ID
    INNER JOIN BOMCost AS bc
        ON bqd.TRANNUM = bc.Assembly

)


-- Statement that executes the CTE
SELECT DISTINCT Assembly, Component, PriceFromPO, Qty, BOMLevel
FROM BOMCost

Demo: SQL Fiddle

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top