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 3788416
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:59:42+00:00 2026-05-19T11:59:42+00:00

I have an interesting SQL problem. I have a hierarchic table of parts that

  • 0

I have an interesting SQL problem. I have a hierarchic table of parts that make a bill of material. similar to this:

ASSEMBLY
---------
parent_part_id
part_id
quantity

I get the hierarchy of this structure with a query like this:

SELECT level, part_id, quantity
from assembly
start with parent_part_id = 1
connect by parent_part_id = prior part_id;

the output might look like this:

level  part_id  quantity
-----  -------  ---------
1      2        2
2      3        10
1      4        2
2      5        1    
3      3        5

so far so good.

the question is this: how do I calculate the total number of each part required in order to make the top level assembly (part 1)?

Grouping this result set by part and summing the quantity is not correct, since the quantity should be multiplied by the quantity of the part immediately above the current part in the hierarchy, recursively up the tree.

I am thinking this is a LAG function, but having trouble visualizing it.

edit: expected results:

part_id  quantity
-------  --------
2        2
3        30
4        2
5        2

more edit: i get interesting results with this query

SELECT rownum, level lvl, part_id, quantity, unit_of_measure
                , connect_by_isleaf || sys_connect_by_path(quantity,'*') math
            from assembly
            start with parent_part_id = 1
            connect by parent_part_id = prior part_id

the math column returns a string representation of the calculation i want to perform 🙂 for instance it may say:

1*1*2*10

or something similar and appropriate… perhaps making a function to parse this and return the result will solve the problem.. anyone think this is outrageous?

  • 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-19T11:59:43+00:00Added an answer on May 19, 2026 at 11:59 am

    In Oracle 11 R2 its possible with a common table expression:

    The test data:

    --  drop table assembly;
    
    create table assembly (
      part_id              number, 
      parent_part_id       number,
      quantity             number
    );
    
    insert into assembly values (2, 1,  2);
    insert into assembly values (3, 2, 10);
    insert into assembly values (4, 1,  2);
    insert into assembly values (5, 4,  1);
    insert into assembly values (3, 5,  5);
    

    The select statement:

    select 
      part_id, 
      sum(quantity_used) as quantity
    from (
      with assembly_hier (lvl, part_id, quantity, quantity_used) as (
        select 
          1        lvl,
          part_id,
          quantity ,
          quantity        quantity_used
        from
          assembly
        where
          parent_part_id = 1 
      union all
        select
          assembly_hier.lvl      + 1 lvl,
          assembly     .part_id,
          assembly     .quantity,
          assembly_hier.quantity_used * assembly.quantity quantity_used
        from
          assembly_hier, assembly
        where
          assembly_hier.part_id = assembly.parent_part_id
      )
      select * from assembly_hier
    )
    group by part_id
    order by part_id;
    

    Edit Prior to Ora11R2, it might work with a model clause:

    select 
      part_id,
      sum(quantity) quantity 
    from (
      select
        lvl
        parent_part_id,
        part_id,
        quantity
      from (
        select 
          lvl,
          parent_part_id,
          part_id,
          quantity
        from (
          select  
            rownum r, 
            level lvl, 
            parent_part_id,
            part_id, 
            quantity
          from 
            assembly
          start with parent_part_id = 1
          connect by parent_part_id = prior part_id
        )
      )
      model
        dimension by (lvl, part_id)
        measures (quantity, parent_part_id)
        rules upsert (
           quantity[     any, any          ] order by lvl, part_id =   quantity[cv(lvl)  , cv(part_id)] * 
                                              nvl( quantity[cv(lvl)-1,    parent_part_id[cv(lvl), cv(part_id)] ], 1)
        )
    )
    group by part_id
    order by part_id;
    

    Edit II Another possibility would be to sum the logarithms of quantity and then take the sum’s exponent:

    select 
      part_id,
      sum(quantity) quantity
    from (
      select 
        part_id,
        exp(sum(quantity_ln) over (partition by new_start order by r)) quantity
      from (
        select 
          r,
          lvl,
          part_id,
          quantity_ln,
          sum(new_start) over(order by r) new_start
        from (
          select 
            rownum r, 
            level lvl, 
            part_id, 
            ln(quantity) quantity_ln,
            nvl(lag(connect_by_isleaf,1) over (order by rownum),0) new_start
          from assembly
          start with parent_part_id = 1
          connect by parent_part_id = prior part_id
        )
      )
    )
    group by part_id
    order by part_id
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interesting SQL problem that I need help with. Here is the
We have an interesting table query (SQL Server 2008) that fails with a different
We have an interesting problem with WCF binding and streaming transfer mode that we
Here is one very interesting problem. I am using SQL Server 2008. I have
Here's an interesting problem, I have a list of users that I list in
I have a very interesting problem. I have a table where I have data,
I faced an interesting problem as... I have MSSQL (2005) table A which contains
Here's an interesting problem. I have an ETL script written in c# that I
I'm using SQL Server 2008. I have an interesting scenario where a stored procedure
I found a very interesting/strange thing about MAX() function in SQL. I have column

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.