Click here to Skip to main content
15,884,388 members
Articles / Database Development

SQL Bill of Materials Example

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
7 Mar 2022CPOL4 min read 10.3K   3  
Puzzle to explore a SQL BOM example
In this post, you will learn how to explore a SQL bill of materials example by solving a puzzle.

In this puzzle, we’ll explore a SQL bill of materials example. Companies use a BOM (Bill of Materials) to itemize the components and sub assemblies used to construct their products.

Solving puzzles is a great way to learn SQL. Nothing beats practicing what you’ve learned. Once you have figured out the puzzle, post your answer in the comments so we all can learn from one another.

Bill of Materials Problems

AdventureWorks Bill of Materials Schema

Using the AdventureWorks database and diagram above, provide queries for the following:

  1. Write a query to count the number of products not listed in BillOfMaterials
  2. Retrieve Product IDs, including Names and ProductNumbers, that have no subcomponents
  3. Return Product IDs, including Names and ProductNumbers, that aren’t in a subcomponent
  4. List Product IDs, including Names and ProductNumbers, that are a subcomponent

If you need help understanding how a BOM works, please check out the following diagram:

AdventureWorks Bill of Materials Example

The items in:

  • yellow are products that aren’t in a subcomponent.
  • green are sub components of the Bike product.
  • blue are parts that don’t have any subcomponents.

Hang in there! By the time you get done with these SQL bill of materials examples, you’ll be a pro!

Write a Query to Count the Number of Products Not Listed in BillOfMaterials

To know the number products not list on the BOM, we need to find out how many products are in the aren’t listed in the BOM as a product assembly or component .

One way to do this is to use a subquery in the where clause and look for ProductID references that don’t exist in either the BillofMaterial table ProductAssemblyID or ComponentID columns (bold text).

Also, I’ve highlighted in bold the SQL to not include discontinued or inactive products.

The query to do so is:

SQL
SELECT Count(1)
FROM   Production.Product P
WHERE  P.SellEndDate is NULL
       AND p.DiscontinuedDate is NULL
       AND NOT EXISTS
       (SELECT 1
        FROM   Production.BillOfMaterials BOM
        WHERE  BOM.ProductAssemblyID = p.ProductID
               OR BOM.ComponentID = p.ProductID
       )

This returns the scalar value 157.

The subquery and main query are correlated via ProductID. A match by ProductID is made to the BOM’s components and assemblies for each Product.

To do this, we run a query for each product. We match the product’s ProductID to BOM entries. It is included in the result if it matches a ComponentID or ProductAssemblyID’.

List Product IDs, including Names and ProductNumbers, that have no subcomponents

To answer this question, we take our query from the previous answer alter it to display product column values, rather than a row count.

We can easily infer that a product doesn’t have subcomponents if the product isn’t listed in the BOM table as an assembly, since a subcomponent is any product used within another one. You’ll see we used a subquery (bold text) to search for product assemblies.

If no ProductAssemblyIDs are found, the NOT EXISTS operator returns TRUE.

SQL
SELECT P.ProductID,
       P.Name,
       P.ProductNumber,
       P.FinishedGoodsFlag,
       P.ListPrice
FROM   Production.Product P
WHERE  P.SellEndDate is NULL
       AND p.DiscontinuedDate is NULL
       AND NOT EXISTS (SELECT 1
                       FROM  Production.BillOfMaterials BOM
                       WHERE P.ProductID = BOM.ProductAssemblyID)

List Product IDs, including Names and ProductNumbers, that aren’t in a subcomponent

The main difference in answering this question versus the previous lies in the subquery used to probe the BillOfMaterials table.

For this question, we’re concerned whether it is not a subcomponent. To do so, we alter our subquery to search for BOM entries whose ComponentID matched the ProductID. Finding a match here signifies our product is a subcomponent. Of course, we’re looking for the opposite, so we use the NOT EXISTS qualifier to make it so.

SQL
SELECT P.ProductID,
       P.Name,
       P.ProductNumber,
       P.FinishedGoodsFlag,
       P.ListPrice
FROM   Production.Product P
WHERE  P.SellEndDate is NULL
       AND p.DiscontinuedDate is NULL
       AND NOT EXISTS (SELECT 1
                       FROM  Production.BillOfMaterials BOM
                       WHERE P.ProductID = BOM.ComponentID)

List Product IDs, including Names and ProductNumbers, that are a subcomponent

The answer to this question is just the opposite to that of the previous one. So, in this case, the subquery searches for BOM entries whose ComponentID matched the ProductID.

SQL
SELECT P.ProductID,
       P.Name,
       P.ProductNumber,
       P.FinishedGoodsFlag,
       P.ListPrice
FROM   Production.Product P
WHERE  P.SellEndDate is NULL
       AND p.DiscontinuedDate is NULL
       AND EXISTS <span style="color: #008000;">(SELECT 1
                   FROM  Production.BillOfMaterials BOM
                   WHERE P.ProductID = BOM.ComponentID
                  )</span>

Final Comments on AdventureWorks Bill of Materials

You may be wondering why I use subqueries in my answers rather than joins. I did so since I think in this sort of problem, the subquery is easier to read. Since we’re testing for existence, the EXISTS clause lends nicely to this, and may be easier to interpret.

Also, by using subqueries, I’m also including products that don’t have any entries in the BOM table. If my answer used INNER JOINS, then by virtue they only include matching rows, these products which don’t have bill of materials would have been excluded.

Of course, I could have use Outer JOINS and tested for NULL to skirt the issue, but for some reason, I try to avoid outer joins when I can. That’s just my preference, and not necessarily a best practice.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Easy Computer Academy, LLC
United States United States
Hello my name is Kris. I’m here because I am passionate about helping non-techie people to overcome their fear of learning SQL.

I know what it is like to not know where to start or whether the time spent learning is worth the effort. That is why I am here to help you to:
- Get started in an easy to follow step-by-step manner.
- Use your time wisely so you focus on what is important to learn to get the most value from your time.
- Answer your questions. Really! Just post a comment and I’ll respond. I’m here to help.

It wasn’t long ago that I was helping a colleague with some reporting. She didn’t know where to start and soon got overwhelmed and lost as she didn’t know SQL.

I felt really bad, as she was under pressure to get some summary information to her boss, the built-in reports were falling short, and to make them better would require her to know SQL. At that time that seemed impossible! It in dawned on me, it doesn’t have to be that way.

Then I discovered a way for anyone with the desire to easily learn SQL. I worked with my co-worker, started to teach her what I learned and soon she was able to write reports and answer her boss’ questions without getting stressed or ploughing hours into manipulating data in Excel.

It hasn’t always been easy. Sometimes the information seems abstract or too conceptual. In this case I’ve found out that a visual explanation is best. I really like to use diagrams or videos to explain hard-to-grasp ideas.

Having video, pictures, and text really help to reinforce the point and enable learning.

And now I want to help you get the same results.

The first step is simple, click here http://www.essentialsql.com/get-started-with-sql-server/

Comments and Discussions

 
-- There are no messages in this forum --