Sorry for the slightly obscure Title. I couldn't really think of a way to describe my scenario accurately, but here goes:
The following bit of code retrieves the latest bill of materials for a part: -
SELECT b.BOMPartID, b.ComponentPartID, b.Quantity, b.ScrapPercent
-
FROM BillOfMaterials b
-
JOIN Versions v ON v.VersionID = b.VersionID
-
WHERE v.StatusID = 1
-
AND b.PrimeComponent = 1
-
My problem is that the ComponentPartID may also have its own bill of materials and I want to end up with just a single list of all components required on the chosen part.
I have tried using multiple LEFT JOINs for the BillOfMaterials table (where the ComponentPartID in the first table is linked to the BOMPartID in the second copy of the table) and I almost get what I want. -
SELECT b1.BOMPartID,
-
CASE
-
WHEN b7.ComponentPartID IS NOT NULL THEN b7.ComponentPartID
-
WHEN b6.ComponentPartID IS NOT NULL THEN b6.ComponentPartID
-
WHEN b5.ComponentPartID IS NOT NULL THEN b5.ComponentPartID
-
WHEN b4.ComponentPartID IS NOT NULL THEN b4.ComponentPartID
-
WHEN b3.ComponentPartID IS NOT NULL THEN b3.ComponentPartID
-
WHEN b2.ComponentPartID IS NOT NULL THEN b2.ComponentPartID
-
END
-
FROM BillOfMaterials b1
-
LEFT JOIN BillOfMaterials b2 ON b2.BOMPartID = b1.ComponentPartID
-
LEFT JOIN BillOfMaterials b3 ON b3.BOMPartID = b2.ComponentPartID
-
LEFT JOIN BillOfMaterials b4 ON b4.BOMPartID = b3.ComponentPartID
-
LEFT JOIN BillOfMaterials b5 ON b5.BOMPartID = b4.ComponentPartID
-
LEFT JOIN BillOfMaterials b6 ON b6.BOMPartID = b5.ComponentPartID
-
LEFT JOIN BillOfMaterials b7 ON b7.BOMPartID = b6.ComponentPartID
-
but this leaves me with lots of seemingly repeated code (which I am sure should not be necessary) and another problem:
The number of sub assemblies that exist may vary from 0 to ??. From running through the LEFT JOIN I can see that currently there are up to 6 levels but there is nothing to stop the bill of materials having any number.
I also can't get the 'WHERE' conditions working in this version but I have not tried many things as I don't think this is the way I should be doing it anyway.
Any ideas on how to achieve what I am after would be much appreciated!
4 1843
Looks like you're trying to grab the first non-null value. Will COALESCE() work?
-- CK
The problem is more with getting the full BOM e.g. table contains the following
BomPartID, ComponentPartID, StatusID, PrimeComponent
PartA, PartB, 1, 1
PartA, PartC, 1, 1
PartA, PartD, 1, 0
PartB, PartE, 1, 1
PartB, PartF, 2, 1
PartE, PartG, 1, 1
PartF, PartH, 1, 1
Result set wanted is (linking it to a Part Master table so all parts are included even if they are not in the BOM table):
PartA, PartB
PartA, PartC
PartA, PartE (because it is a sub component of PartB)
PartA, PartG (because it is a sub component of PartE)
PartB, PartE
PartC, Null (no sub components)
PartD, Null
PartE, PartG
PartF, PartH
The entries:
PartA, PartD would not exist as PartD is not a PrimeComponent
PartB, PartF would not exist because it is not the 'current' version
A Common Table Expression might be helpful for this requirement. Something I'm thinking of creating a sample but seems to not have the time to do, sorry :)
-- CK
Thanks so much for the pointer.
A Common Table Expression might be helpful
I found a good starting point here.
Having read that I have come up with code as follows which is pretty much there - need to tweak it a little still but that should be fine - WITH MyBOM (BOMPartID, ComponentPartID, Quantity, ScrapPercent, Level)
-
AS
-
(
-
SELECT b.BOMPartID, b.ComponentPartID, b.Quantity, b.ScrapPercent, 1 AS Level
-
FROM Structure.BillOfMaterials b
-
JOIN Structure.Versions v ON v.VersionID = b.VersionID
-
WHERE v.StatusID = 1
-
AND b.PrimeComponent = 1
-
-- AND b.BOMPartID = 192
-
UNION ALL
-
SELECT b.BOMPartID, b.ComponentPartID, b.Quantity, b.ScrapPercent, Level + 1
-
FROM Structure.BillOfMaterials b
-
JOIN Structure.Versions v ON v.VersionID = b.VersionID
-
INNER JOIN MyBOM m ON b.BOMPartID = m.ComponentPartID
-
WHERE v.StatusID = 1
-
AND b.PrimeComponent = 1
-
)
-
SELECT p1.PartNumber, p2.PartNumber, Quantity, ScrapPercent, Level
-
FROM myBOM m
-
LEFT JOIN Structure.Parts p1 ON p1.PartID = m.BOMPartID
-
LEFT JOIN Structure.Parts p2 ON p2.PartID = m.ComponentPartID
-
ORDER BY m.Level
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Rolf Wester |
last post by:
Hi,
I have a form with a select element with multiple="true". When using the
GET method (I suppose the same happens with the POST method) I can seen
that the form sends channels=CH1&channels=CH2...
|
by: Vikas Vijay |
last post by:
hello all,
I have a c++ code in which is sth like
while(i < N)
{
....
do some processing in c++ to create file abc
system("simulator_name < abc")
......c++ ... processing......
}
|
by: Ben Hallert |
last post by:
Hi guys,
I'm trying to figure out what bone headed mistake I made on something I
put together. I've got a form (named 'context') that has a variable
number of select-multiple inputs on it. ...
|
by: Bonge Boo! |
last post by:
This has got to be obvious, but I can't make it work.
I have a form called with 3 pull down menus. They are linked to a database
which generates the values for the <SELECT? Pull-downs.
Lets...
|
by: areef.islam |
last post by:
Hi, I am kinda new to javascript and I am having this problem with
selecting multiple options from a select tag. Hope someone can help me
out here.
here is my code...
|
by: abhishekhs |
last post by:
Hi all
I have more than one multiple select lists in a page. Something like this
<tr>
<td>
<select NAME="StrainList" ID="StrainList" SIZE="5" multiple="multiple" style="width: 150px">
<?...
|
by: John A Grandy |
last post by:
Is it possible to write a foreach so that it simultaneously iterates through
two collections ( or two properties of the same collection ) ?
Following is *only meant as an example* :
-- iterate...
|
by: charlie.xia.fdu |
last post by:
Hi C++ users,
for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {}
example from: http://www.tech-faq.com/iterations.shtml
Is not valid in my eclipse cdt.
Is there multiple initialization in C++?...
|
by: phpnewbie26 |
last post by:
My current form has one multiple select drop down menu as well as few other drop down menus that are single select. Originally I had it so that the multiple select menu was first, but this created...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |