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

  • Home
  • SEARCH
  • 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 502745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:19:22+00:00 2026-05-13T06:19:22+00:00

I need to recompute quantities in a BOM structure which only guarantees correct quantity

  • 0

I need to recompute quantities in a BOM structure which only guarantees correct quantity values for leaf nodes.

If the above is a bit blurry don’t worry, below is a simplified example.

Consider a hierarchical table defining three columns: ID (PK), PID (parent ID in the hierarchy) and Q (quantity – of something, for the current record).

set nocount on
declare @results table(
        ID  nvarchar(3), 
        PID nvarchar(3), 
        Q   int,
        lvl int,
        ord int
    )

The ord column serves only to sort results.

The lvl column defines the level for the current record in the hierarchy.

For those wondering how these columns are maintained, the @results table is populated in the real world by a function which does all the tricks; for this example they’ll be given hardcoded values.

Now, the problem is that correct Q values are guaranteed only for leaf-level nodes in the hierarchy. For the other nodes Q may or may be not correctly defined. My task is to recompute Q values for these nodes.

Sample data:

insert into @results(ord, lvl, ID, PID, Q)
select 1, 0, 'A' as ID, null as PID, null as Q union
select 2, 1, 'B'  , 'A' , 15 union
select 3, 1, 'C'  , 'A' , 10 union
select 4, 2, 'B1' , 'B' , 6  union
select 5, 2, 'B2' , 'B' , 4  union
select 6, 2, 'C1' , 'C' , 5  union
select 7, 2, 'C2' , 'C' , 3  union
select 8, 3, 'C11', 'C1', 4  union
select 9, 3, 'C12', 'C1', 3

As you may see, the quantities for B and C1 are wrong: they’re 15 and 5 but should be 10 and 7:

select * from @results order by ord

Here’s the initial data:

ID   PID            Q         lvl         ord
---- ---- ----------- ----------- -----------
A    NULL        NULL           0           1
B    A             15           1           2
C    A             10           1           3
B1   B              6           2           4
B2   B              4           2           5
C1   C              5           2           6
C2   C              3           2           7
C11  C1             4           3           8
C12  C1             3           3           9

Finally, the question: is there any way to update this table in a set-based manner so all quantities are updated with bottom-up summed quantities of children nodes?

The best I came with can be seen below, but it’s not set based:

declare @level int

select @level = max(lvl) from @results

while (@level > 0)
begin
    update r set Q = s.SumQ
    from @results r inner join (
            select PID, sum(Q) as SumQ 
            from   @results 
            where lvl = @level group by PID
        ) s on ( r.ID = s.PID )

    set @level = @level - 1
end

select * from @results

which gives the correct quantities:

ID   PID            Q         lvl         ord
---- ---- ----------- ----------- -----------
A    NULL          20           0           1
B    A             10           1           2
C    A             10           1           3
B1   B              6           2           4
B2   B              4           2           5
C1   C              7           2           6
C2   C              3           2           7
C11  C1             4           3           8
C12  C1             3           3           9

Thanks!

  • 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-13T06:19:23+00:00Added an answer on May 13, 2026 at 6:19 am
    ;WITH   q AS
            (
            SELECT  *, id AS initial
            FROM    @results
            UNION ALL
            SELECT  r.*, initial
            FROM    q
            JOIN    @results r
            ON      r.pid = q.id
            )
    UPDATE  ru
    SET     q = qn.nq
    FROM    @results ru
    JOIN    (
            SELECT  initial,
                    SUM(rq) AS nq
            FROM    q
            LEFT JOIN
                    (
                    SELECT  id,
                            CASE
                            WHEN EXISTS
                            (
                            SELECT  NULL
                            FROM    @results ri
                            WHERE   ri.pid = r.id
                            )
                            THEN NULL
                            ELSE q
                            END AS rq
                    FROM    @results r
                    ) r
            ON      r.id = q.id
                    AND r.id <> q.initial
            GROUP BY
                    q.initial
            ) qn
    ON      ru.id = qn.initial
            AND qn.nq IS NOT NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 402k
  • Answers 402k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Go for content related names - those would be easy… May 15, 2026 at 4:51 am
  • Editorial Team
    Editorial Team added an answer Look at the Page.FindControl method. How To Access One UserControl… May 15, 2026 at 4:51 am
  • Editorial Team
    Editorial Team added an answer SO_BINDTODEVICE socket option is not standard and is not supported… May 15, 2026 at 4:51 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.