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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:08:20+00:00 2026-05-27T03:08:20+00:00

I have hierarchical data (right) in table in following manner which creates Hierarchy as

  • 0

I have hierarchical data (right) in table in following manner which creates Hierarchy as shown in left. Tables are kept in oracle 11g.

TREE Hierarchy          Tree Table  
--------------          Element Parent
                        ------  ------
P0                      P0  
    P1                  P1      P0
        P11             P2      P0
            C111        P11     P1
            C112        P12     P1
        P12             P21     P2
            C121        P22     P2
            C122        C111    P11
    P2                  C112    P11
        P21             C121    P12
            C211        C122    P12
            C212        C211    P21
        P22             C212    P21
            C221        C221    P22
            C222        C222    P22

My data table has values as follows. It contains values for all leaf nodes.
Data Table

Element Value  
C111    3  
C112    3  
C121    3  
C122    3  
C211    3  
C212    3  
C221    3  
C222    3  
P11     6  

I need to generate insert statement, preferably single insert statement which will insert rows in data table based on sum of values of the children.
Please note we need to calculate sum for only those parents whose value is not present in data table.

Data Table (Expected After Insert)

Element Value
C111    3
C112    3
C121    3
C122    3
C211    3
C212    3
C221    3
C222    3
P11     6

-- Rows to insert
P12     6
P21     6
P22     6
P1      12
P2      12
P0      24
  • 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-27T03:08:21+00:00Added an answer on May 27, 2026 at 3:08 am

    If all leaf nodes are at the same height (here lvl=4), you can write a simple CONNECT BY query with a ROLLUP:

    SQL> SELECT lvl0,
      2         regexp_substr(path, '[^/]+', 1, 2) lvl1,
      3         regexp_substr(path, '[^/]+', 1, 3) lvl2,
      4         SUM(VALUE) sum_value
      5    FROM (SELECT sys_connect_by_path(t.element, '/') path,
      6                 connect_by_root(t.element) lvl0,
      7                 t.element, d.VALUE, LEVEL lvl
      8             FROM tree t
      9             LEFT JOIN DATA d ON d.element = t.element
     10            START WITH t.PARENT IS NULL
     11           CONNECT BY t.PARENT = PRIOR t.element)
     12   WHERE VALUE IS NOT NULL
     13     AND lvl = 4
     14   GROUP BY lvl0, ROLLUP(regexp_substr(path, '[^/]+', 1, 2),
     15                         regexp_substr(path, '[^/]+', 1, 3));
    
    LVL0 LVL1  LVL2   SUM_VALUE
    ---- ----- ----- ----------
    P0   P1    P11            6
    P0   P1    P12            6
    P0   P1                  12
    P0   P2    P21            6
    P0   P2    P22            6
    P0   P2                  12
    P0                       24
    

    The insert would look like:

    INSERT INTO data (element, value) 
    (SELECT coalesce(lvl2, lvl1, lvl0), sum_value
       FROM <query> d_out
      WHERE NOT EXISTS (SELECT NULL
                          FROM data d_in
                         WHERE d_in.element = coalesce(lvl2, lvl1, lvl0)));
    

    If the height of the leaf nodes is unknown/unbounded this gets more hairy. The above approach wouldn’t work since ROLLUP needs to know exactly how many columns are to be considered.

    In that case, you could use the tree structure in a self-join :

    SQL> WITH HIERARCHY AS (
      2     SELECT t.element, path, VALUE
      3       FROM (SELECT sys_connect_by_path(t.element, '/') path,
      4                    connect_by_isleaf is_leaf, ELEMENT
      5                FROM tree t
      6               START WITH t.PARENT IS NULL
      7              CONNECT BY t.PARENT = PRIOR t.element) t
      8       LEFT JOIN DATA d ON d.element = t.element
      9                       AND t.is_leaf = 1
     10  )
     11  SELECT h.element, SUM(elements.value)
     12    FROM HIERARCHY h
     13    JOIN HIERARCHY elements ON elements.path LIKE h.path||'/%'
     14   WHERE h.VALUE IS NULL
     15   GROUP BY h.element
     16   ORDER BY 1;
    
    ELEMENT SUM(ELEMENTS.VALUE)
    ------- -------------------
    P0                       24
    P1                       12
    P11                       6
    P12                       6
    P2                       12
    P21                       6
    P22                       6
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hierarchical table on Oracle pl/sql. something like: create table hierarchical (
I have the following hierarchical table: Table Category: CategoryId, ParentCategoryId, CategoryName 1, null, SomeRoot
I currently have a closure table used for hierarchical data that has 5 million
I have some hierarchical data that I need display as a table, can I
I have a hierarchical data structure which I'm displaying in a webpage as a
I have hierarchical data in a nested set model (table:projects): My table (projects): id,
I have hierarchical data stored in the datastore using a model which looks like
Working with SQL Server 2008 R2. I have a table of hierarchical data, with
I have a hierarchical data structure which, as far as I can see, needs
I have data structured in a hierarchical manner, that when being rendered with 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.