Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7045297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:30:49+00:00 2026-05-28T02:30:49+00:00

I have a problem I tried solving on my own, but didn’t get far

  • 0

I have a problem I tried solving on my own, but didn’t get far (tried different approaches but all of them ended up in a big wall of code, that eventually did not do what I needed). So I’m here asking for any advice, solution, code snippets you can offer. Thanks in advance!

I have an SQL db with tables PARTS, ASSEMBLIES and FORMULA. As names suggest assemblies are made out of parts but can also be made out of other assemblies. How assemblies are made is written in table FORMULA. In my application I can enter how many assemblies have been made (increase stock). But when I increase stock for an assembly, I need to decrease stock for corresponding parts and (sub)assemblies. For better understanding I’ll illustrate this with an example;

PARTS

partID   partStock
  p1       100
  p2       100
  p3       100

ASSEMBLIES

assID    assStock
ass1        10
ass2        10

FORMULA

assID   parts   isAssembly  quantity
ass1     p1         no          1
ass1     p2         no          2
ass1     ass2       yes         1 
ass2     p3         no          2

Explanation of the table: ass1 is assembled from 1x p1 + 2x p2 + 1x ass2 (which is another assembly) and ass2 is assembled from 2x p3

So when I increase stock of ass1 for 10 (this is not a problem, I can do it), I would also need to decrease stock for p1(10), p2(20), ass2(10). And if stock for ass2 would be <10, I’d need to decrease stock of p3(for a remaining number).

Does anyone even understand what I want to do here? 😀

Any suggestions are welcome, thank you very much! Oh, and I’m writing my web app in C#. 🙂

EDIT: As it has been pointed out in comments, I firstly search for an idea (design pattern) how to accomplish this. Actual SQL queries and coding is not such a big problem.. If anyone has some helpful code, its a bonus. 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:30:49+00:00Added an answer on May 28, 2026 at 2:30 am

    Personally I would start with merging the part and assembly tables, this makes your model and calculations a easier to understand. Especially since an assembly can be a part of an other assembly as well. It will probably also make other things like inventory overviews a lot easier.

    Your tables could look like this:

    PARTS:

    partID, partStock, isAssembly
    p1      100        false
    p2      100        false
    p3      100        false
    ass1    10         true
    ass2    10         true
    

    FORMULA

    partID, part, quantity
    ass1    p1    1
    ass1    p2    2
    ass1    ass2  1
    ass2    p3    2
    

    Technically the ‘isAssembly’ field in the Parts table isn’t required because a part is an assembly when there are formulas for it.

    To update the stock when parts are assembled you can recursively update the stock of the additional parts that need to be build.

    The pseudo code for ‘building’ assemblies will be roughly like this:

    void UpdateStock(string partID, int count)
    {
        formulas = "select * from FORMULA where partID = " + partID;
        foreach(formula in formulas)
        {
            subPart = "select * from PARTS where partID = " + formula.partID;
            subCount = count * formula.quantity;
    
            // Check for sufficient stock of this part
            shortage = (subPart.partStock - subCount) * -1;
            if(shortage > 0)
            {
                // When this part is an assembly we can try to 'build' more.
                if(subPart.isAssembly)
                {
                    UpdateStock(formula.partID, shortage);
                    // Note that when making more isn't possible
                    // this will throw an error.
                }
                else
                {
                    // Otherwise there simple isn't enough.
                    throw Error("Not enough stock");
                }
            }
            // Once we get here there should be enough stock, 
            // either because there was enough in the first place
            // or because we made more. 
            subPart.partStock = subPart.partStock - subCount;
        }
    
        // Lastly increase the actual stock.
        part = "select * from PARTS where partID = " + partID;
        part.partStock = part.partStock + count;
    }
    

    You do need to make sure you don’t save any changes to the database until the whole function is done, otherwise you might have decreased the stock on one part before the function fails because another part is out of stock.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried solving this problem by posting other related questions here that focused
I have tried this approach/hack: http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/ The problem is this leaves a faint seam.
I have problem with return statment >.< I want to store all magazine names
First of all: I have fruitlessly tried searching Stackoverflow.com for any clues on my
I'm solving a problem that consist in generate the first n<1000000 lucky numbers, but
I tried solving my problem using this link update - I figured out that
I have a problem and I found nothing in my searches that's solving it.
I am solving UVa site problems, I have a problem that I must sort
Working on a problem that requires a GA. I have all of that working
I have written a forum, but I've got one problem... The text from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.