SQL Running Value Multiplication

So I have a multi-level BOM in which I am able get all my results.  However, I am struggling to get my calculation.  I am to find all purchased materials that make up the fabricated item.  I had put this into my temp table, but I need to calculate and only output my purchased material and total usage.

Temp Table
So with the above temp table, I have 2 fabricated material A1 and G1.  Parent ID A1 consist of B2, C2, and D2 which are all fabricated parts as well.  Each of those components required a purchased material to make.  So I have my level on how deep it is.  Then  you can link the Component ID to the Item ID and it'll take you to the last purchased material.  My issue is the calculation.  Example look at purchased material "F3".  This require .004 to make B2.  B2 require 2 in A1.  In total I should have .008.  However, with my query I am not getting this result.

Here's my expected result.
Final Result
Not sure if I am not doing something right with my query.

DECLARE @TEMP TABLE(
		ITEM_ID VARCHAR(30),
		COMPONENT_ID VARCHAR(30),
		QTY DECIMAL(8,4),
		FAB_PURC VARCHAR(10),
		PARENT_ID VARCHAR(30),
		LVL INT
		);

INSERT INTO @TEMP
VALUES(NULL, NULL, 1, 'FAB', 'A1', 0),
('A1', 'B2', 2, 'FAB', 'A1', 1),
('A1', 'C2', 1, 'FAB', 'A1', 1),
('A1', 'D2', 1, 'FAB', 'A1', 1),
('B2', 'F3', .004, 'PURC', 'A1', 2),
('C2', 'E3', .1324, 'PURC', 'A1', 2),
('D2', 'E3', .0765, 'PURC', 'A1', 2),
(NULL, 'G1', 1, 'FAB', 'G1', 0),
('G1', 'H2', 4, 'FAB', 'G1', 1),
('G1', 'I2', 2, 'FAB', 'G1', 1),
('G1', 'J2', 2, 'FAB', 'G1', 1),
('G1', 'K2', 2, 'FAB', 'G1', 1),
('H2', 'M3', .0394, 'PURC', 'A1', 2),
('I2', 'M3', .03, 'PURC', 'A1', 2),
('J2', 'M3', .027, 'PURC', 'A1', 2),
('K2', 'M3', .0013, 'PURC', 'A1', 2)

SELECT	T.ITEM_ID, T.COMPONENT_ID, T.QTY, T.FAB_PURC, T.PARENT_ID, T.LVL, RT.RUNNING_VALUE
FROM	@TEMP T
		CROSS APPLY
		(
		SELECT	EXP(SUM(LOG(T2.QTY))) RUNNING_VALUE
		FROM	@TEMP T2
		WHERE	T2.PARENT_ID = T.PARENT_ID
				AND T2.COMPONENT_ID <= T.COMPONENT_ID
		) RT

Open in new window

holemaniaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
Hi.

I think you are looking for something like this.

;WITH mlBOM 
AS 
(
	/* anchor query is first level of BOM (finished goods). */
	SELECT T.ITEM_ID, T.COMPONENT_ID, T.QTY, T.FAB_PURC, T.PARENT_ID, T.LVL
	FROM @TEMP T
	WHERE LVL = 0

	UNION ALL

	/* recurse through BOM. */
	SELECT T.ITEM_ID, T.COMPONENT_ID, CONVERT(DECIMAL(8,4), T.QTY * P.QTY), T.FAB_PURC, T.PARENT_ID, T.LVL
	FROM @TEMP T
	JOIN mlBOM P ON T.ITEM_ID = COALESCE(P.COMPONENT_ID, P.PARENT_ID)
)
SELECT COMPONENT_ID, QTY = SUM(QTY)
FROM mlBOM
WHERE FAB_PURC = 'PURC'
GROUP BY COMPONENT_ID
;

Open in new window


At least I typically will use recursive common table expressions when building multi-level BOMs.  However, you may be after something different here, depending on what you were using LOG and EXP for.  An alternative approach simply is the below.

SELECT C.COMPONENT_ID
     , QTY = CONVERT(DECIMAL(8,4), SUM(C.QTY * P.QTY))
FROM @TEMP C
JOIN @TEMP P ON C.ITEM_ID = COALESCE(P.COMPONENT_ID, P.PARENT_ID)
WHERE C.FAB_PURC = 'PURC'
GROUP BY C.COMPONENT_ID
;

Open in new window


I hope those help.

P.S. The solution in a nutshell is to carry the quantity per assembly down through the BOM by multiplying each level's quantity per by the quantity per of the level above.  Therefore, the second solution may have issues when same component appears at multiple levels.  Hence, the reason I typically will do this through a CTE then aggregate at the end.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
holemaniaAuthor Commented:
Awesome.  Thank you so much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.