Hierarchical data, how to aggregate over levels in hierarchical query?

Hello

I hope someone can help me.

I held in a data table ("" what part was built in what other part of when when? "')
ID parent_id build_in build_out
1 NULL NULL NULL
2/1 2010 2012
3 2 2011 2013
4 2 2013 NULL

What are the parts is stored in a separate table.

Now I want to know when when which part was built in the first level, in the example, I want to know that
1 was simply a part of 1
2 was part of 1 between 2010 and 2012
3 was part of 1 between 2011 and 2012
4 has never been a part of 1

I tried several approaches - if there is a fixed number of levels between the types, I am interested I can do using joins and more/less (similarly as some nvl). Unfortunately this is not always the case (some parts appear on several levels).
If I'm only interested in the parts that are never deleted, I can get by using a style of connect request and get the current configuration. But I can't seem to understand the time connecting part to the high level in such a query, or even filtering absurd combinations (like "4 in 1" in the example above). I could deal with the data recovered outside the database, but I prefer not.

Is there a way to obtain the hierarchical data with an aggregate (min, max) for all levels?

What version of Oracle you are on?

In 11.2.x, you can use the recursive subquery factoring. Something like

with t (id, parent_id, build_in, build_out) as (
select 1, null, null, null from dual union all
select 2, 1, 2010, 2012 from dual union all
select 3, 2, 2011, 2013 from dual union all
select 4, 2, 2013, null from dual
)
, c1 (root_id, id, parent_id, build_in, build_out) as (
select id, id, parent_id, 0, 9999
from t
where parent_id is null
union all
select root_id, t.id, t.parent_id
, greatest(nvl(t.build_in,0), nvl(b.build_in,0))
, least(nvl(t.build_out,9999), nvl(b.build_out,9999))
from c1 b, t
where b.id = t.parent_id
)
select * from c1
where build_in < build_out
;
ROOT_ID ID    PARENT_ID  BUILD_IN  BUILD_OUT
------- ----- ---------- --------- ----------
1       1                0         9999
1       2     1          2010      2012
1       3     2          2011      2012      

With a hierarchical query by using the syntax connection, you could do something like

select * from (
select connect_by_root id as root, id
, greatest(nvl(build_in,0), nvl(prior build_in,0)) as max_in, least(nvl(build_out,9999), nvl(prior build_out,9999)) as min_out
from t
start with parent_id is null
connect by parent_id = prior id
)
where max_in < min_out
;

but it is not powerful enough. This version compares the dates between a current and previous levels, but the recursive subquery is to compare the dates in the current level for the winners of the comparisons to the previous level. Not sure if it's an important distinction for your needs, however.

If you are on 11.2 I advise to use the recursive subquery factoring. If this isn't the case, you can try the link by version.

Kind regards
Bob

Tags: Database

Similar Questions

  • How formula MDX on level zero member data to Smartview?

    Hi Experts,

    I'm a member of level zero, which stores the data AND formula MDX to ASO.  I wonder what happens when I pick up this member using Smartview?  ASO runs this formula, stores the result in the form of data in this member (zero level) - then report stored data to Excel? In other words - how formula MDX on members level report data zero to Smartview?

    Merry Christmas / Happy Hanakkuk (and all other religious holiday greeting comes to be PC).

    Ho Ho Ho!

    Level 0 members not also store data, if it has an MDX formula. The value is calculated according to the formula only at the time of the recovery.

  • How to recover the level of hierarchy parent set in the SQL query?

    I use Oracle 10.2.0.4 version. I have a database with the following structure table,
    CREATE TABLE SOME_TABLE
    ( 
      ID        NUMBER(10) PRIMARY KEY,
      PARENT    NUMBER(10)
    )
    
    
    ID    PARENT_ID
    
    1        0 
    2        1
    3        1
    4        2
    5        4
    6        4
    100      0
    This is root PARENT_ID ' 0'. Parent to say ID 5 is 4 & 4 parent is 2 & 2 parent is 1. How do I frame a SQL select queries to who can possibly give me the output as "5/4/2/1" which is the parent of ID hierarchy 5. " See that the output does not contain the root PARENT_ID ' 0'

    Published by: Vel January 5, 2012 12:20 AM

    Barros says:
    Frank,

    I used the Sub hierarchical query to achieve the result desired, mentioned above,.

    select SYS_CONNECT_BY_PATH(parent,'/') root ,level from some_table
    start with id = 5
    CONNECT BY PRIOR parent =  id;
    
    o/p
    
    /4     1
    /4/2     2
    /4/2/1     3
    /4/2/1/0     4
    

    But please let me know if it is possible to get the details of the hierarchy (using hierarchical queries) of the query node to the root, as the below a node

    1/2/3/4/5 -- root hierarchy for node 5
    

    OK, you had a go, and you're almost there. Now you seem to be saying you want the hierarchy from the top down rather than bottom-up, so you'll need to change around your connection by the clause. Something like that...

    SQL> ed
    Wrote file afiedt.buf
    
      1  with t as (select 1 as ID, 0 as PARENT_ID from dual union all
      2             select 2, 1 from dual union all
      3             select 3, 1 from dual union all
      4             select 4, 2 from dual union all
      5             select 5, 4 from dual union all
      6             select 6, 4 from dual union all
      7             select 100, 0 from dual)
      8  --
      9  -- end of test data
     10  --
     11  select ltrim(SYS_CONNECT_BY_PATH(id,'/'),'/') root
     12  from   t
     13  where connect_by_isleaf = 1
     14  and id = 5
     15  connect by parent_id = prior id
     16* start with parent_id = 0
    SQL> /
    
    ROOT
    -------------------------------------------------------------------
    1/2/4/5
    
  • I know how to restore my computer to an earlier date, HOW THEN I CLEAR DATE I DON'T WANT?

    I know how to restore my computer to an earlier date, (how to clear the dates I'm not?)

    See: http://support.microsoft.com/kb/306084

    This article describes how to use the System Restore tool to restore your computer to a previous state. System Restore takes a "snapshot" of critical system files and some program files and the registry settings and stores this information as restore points. If your computer is not working properly, you can use these points to return Windows XP to a previous state when your computer was working properly restore. It can also change scripts, batch files, and other types of executable files on your computer.

    FYI - http://www.microsoft.com/windowsxp/using/helpandsupport/getstarted/ballew_03may19.mspx

    and

    http://support.Microsoft.com/kb/310405

    I wouldn't count on SR for what anyone aged over 2 or 3 days. It is not a substitute for imaging of your player.

  • How to write a hierarchical query so that only the child nodes are displayed?

    Hi all

    I have a hierarchical query that I use in an area of tree demand APEX and there are nodes that have no children and I am trying to find a way to not display these nodes. Essentially if the user does not have to develop a lot of knots to know that nothing exists at the most detailed level.

    The data are based on the Oracle Fusion FND tables but for example purposes here is enough data to illustrate my question:


    create table APPL_TAXONOMY_HIERARCHY (SOURCE_MODULE_ID varchar2(30), TARGET_MODULE_ID varchar2(30));
    create table APPL_TAXONOMY_TL (module_id varchar2(30), description varchar2(100), user_module_name varchar2(30), language varchar2(5));
    create table APPL_TAXONOMY (MODULE_ID    varchar2(30),    MODULE_NAME    varchar2(30), MODULE_TYPE varchar2(10),    MODULE_KEY varchar2(30));
    create table TABLES (table_name varchar2(30), module_key varchar2(30));
    
    

    insert into APPL_TAXONOMY_TL values ('1', null, 'Oracle Fusion', 'US' );
    insert into APPL_TAXONOMY_TL values ('2', null, 'Financials', 'US' );
    insert into APPL_TAXONOMY_TL values ('3', null, 'Human Resources', 'US' );
    insert into APPL_TAXONOMY_TL values ('20', null, 'Accounts Payable', 'US' );
    insert into APPL_TAXONOMY_TL values ('10', null, 'General Ledger', 'US' );
    
    insert into APPL_TAXONOMY_HIERARCHY values ('1', 'DDDDDDDD');
    insert into APPL_TAXONOMY_HIERARCHY values ('2', '1');
    insert into APPL_TAXONOMY_HIERARCHY values ('3', '1');
    insert into APPL_TAXONOMY_HIERARCHY values ('4', '1');
    insert into APPL_TAXONOMY_HIERARCHY values ('10', '2');
    insert into APPL_TAXONOMY_HIERARCHY values ('20', '2');
    
    insert into APPL_TAXONOMY values ('1', 'Fusion', 'PROD', 'Fusion');
    insert into APPL_TAXONOMY values ('2', 'Financials', 'FAMILY', 'FIN');
    insert into APPL_TAXONOMY values ('10', 'GL', 'APP', 'GL');
    insert into APPL_TAXONOMY values ('3', 'Human Resources', 'FAMILY', 'HR');
    insert into APPL_TAXONOMY values ('20', 'AP', 'APP', 'AP');
    
    insert into tables values ('GL_JE_SOURCES_TL','GL');
    insert into tables values ('GL_JE_CATEGORIES','GL');
    

    My hierarchical query is as follows:

    with MODULES as
    (
    SELECT h.source_module_id, b.user_module_name, h.target_module_id
          FROM APPL_TAXONOMY_HIERARCHY H,
          APPL_TAXONOMY_TL B,
    APPL_TAXONOMY VL
    where H.source_module_id = b.module_id
    and b.module_id = vl.module_id
    and vl.module_type not in ('PAGE', 'LBA')
    UNION ALL
    select distinct table_name, table_name,  regexp_substr(table_name,'[^_]+',1,1) 
    from TABLES --needed as a link between TABLES and APPL_TAXONOMY
    union all
    select module_key as source_module_id, module_name as user_module_name, module_id as target_module_id  from appl_taxonomy VL where VL.module_type = 'APP')
    SELECT  case when connect_by_isleaf = 1 then 0
                when level = 1             then 1
                else                           -1
           end as status,
    LEVEL,
    user_module_name as title,
    null as icon,
    ltrim(user_module_name, ' ') as value,
    null as tooltip,
    null as link
          FROM MODULES
          START WITH source_module_id = '1'
          CONNECT BY PRIOR source_module_id = target_module_id
    ORDER SIBLINGS BY user_module_name;
    

    In Oracle APEX, this gives a tree with the appropriate data, you can see here:

    https://Apex.Oracle.com/pls/Apex/f?p=32581:29 (username: guest, pw: app_1000);

    The SQL of the query results are:

    STATUSTITLE LEVELVALUE

    ---------- ---------- ------------------------------ ------------------------------

    11 oracle FusionOracle Fusion
    -12 financial tablesFinancials
    -13 accounts payableAccounts payable
    04 APAP
    -1General Accounting 3General Accounting
    -14 GLGL
    05 GL_JE_CATEGORIESGL_JE_CATEGORIES
    05 GL_JE_SOURCES_TLGL_JE_SOURCES_TL
    02 human resourcesHuman resources

    The lowest level is the name of the table to level 5. HR is not any level under level 2, in the same way, "AP" (level 4) has nothing below, i.e. no level 5 and that's why I don't want to show these nodes. Is this possible with the above query?

    Thanks in advance for your suggestions!

    John

    Hello

    The following query will include only the nodes of level = 5 and their ancestors (or descendants):

    WITH modules LIKE

    (

    SELECT h.source_module_id

    b.user_module_name AS the title

    h.target_module_id

    To appl_taxonomy_hierarchy:

    appl_taxonomy_tl b

    appl_taxonomy vl

    WHERE h.source_module_id = b.module_id

    AND b.module_id = vl.module_id

    AND vl.module_type NOT IN ('PAGE', "LBA")

    UNION ALL

    SELECT DISTINCT

    table-name

    table_name

    , REGEXP_SUBSTR (table_name, ' [^ _] +')

    From the tables - required as a link between the TABLES and APPL_TAXONOMY

    UNION ALL

    SELECT module_key AS source_module_id

    AS user_module_name module_name

    module_id AS target_module_id

    Of appl_taxonomy vl

    WHERE vl.module_type = 'APP '.

    )

    connect_by_results AS

    (

    SELECT THE CHECK BOX

    WHEN CONNECT_BY_ISLEAF = 1 THEN 0

    WHEN LEVEL = 1 THEN 1

    OF ANOTHER-1

    The END as status

    LEVEL AS lvl

    title

    -, NULL AS icon

    , LTRIM (title, "") AS the value

    -, NULL as ToolTip

    -, Link AS NULL

    source_module_id

    SYS_CONNECT_BY_PATH (source_module_id - or something unique

    , ' ~' - or anything else that may occur in the unique key

    ) || ' ~' AS the path

    ROWNUM AS sort_key

    Modules

    START WITH source_module_id = '1'

    CONNECT BY PRIOR Source_module_id = target_module_id

    Brothers and SŒURS of ORDER BY title

    )

    SELECT the status, lvl, title, value

    -, icon, tooltip, link

    OF connect_by_results m

    WHEN THERE IS)

    SELECT 1

    OF connect_by_results

    WHERE the lvl = 5

    AND the path AS ' % ~' | m.source_module_id

    || '~%'

    )

    ORDER BY sort_key

    ;

    You may notice that subqueries modules and the connect_by_results are essentially what you've posted originally.  What was the main request is now called connect_by_results, and it has a couple of additional columns that are necessary in the new main request or the EXISTS subquery.

    However, I am suspicious of the 'magic number' 5.  Could you have a situation where the sheets you are interested in can be a different levels (for example, some level = 5 and then some, into another branch of the tree, at the LEVEL = 6, or 7 or 4)?  If so, post an example.  You have need of a Query of Yo-Yo, where you do a bottom-up CONNECT BY query to get the universe of interest, and then make a descendant CONNECT BY query on this set of results.

  • How to find the level of oracle objects dependencies...

    Hi,
    
    in oracle if we have to find the dependencies we use "all_dependencies", how ever if
    we have to find level of dependencies then how should we go about it,
    
    example as shown below;
    
    master object : tablel1
    view 1:            v_test using table1
    
    view 2:            v_test2 using v_test
    
    view 3:            v_test3 using v_test2:
    
    
    so in this case
    
    the master table1 is having level of dependency 0
    
    v_test is having level of dependency 1
    
    v_test2 is having level of dependency 2
    
    v_test3 is having level of dependency 3
    
    and so on....
    
    i hope i am clear in my question,
    
    plz assist,
    
    regards
    uv
    Published by: W Nicloei on October 7, 2008 19:53

    You can use a hierarchical query on the USER_ / ALL_ / table of DBA_DEPENDENCIES, i.e.

    select level, a.*
      from dba_dependencies a
     start with referenced_name = 'DEPT'
            and owner = 'SCOTT'
    connect by nocycle prior owner = referenced_owner
                   and prior name = referenced_name
    order siblings by name
    

    Justin

  • I want to update the verson using celluler data how is possible

    I want to update the verson using celluler data how is possible

    WiFi or iTunes > update the iOS on your iPhone, iPad or iPod touch - Apple Support software

  • How to get to level 2 in apple support

    How to get to level 2 in apple support

    < re-titled by host >

    You mean on this forum?

    Answer the questions and eventually you're going to collect enough points to become level 2.

    https://discussions.Apple.com/docs/doc-9251

    He explains

  • How to see the level of ink for a dell v515w printer?

    Separated from this thread.

    How to see the level of ink for a dell v515w printer?

    Hi Albert,

    I appreciate your time display. You can refer to the manual for more information. http://www.Dell.com/Ed/business/p/Dell-V515w/PD

    To better help, contact Dell technical support.

    http://en.community.Dell.com/support-forums/peripherals/

    Thank you.

  • I allredy backup data, how to restore backup of key USB for windows 2008 r2 standard?

    I allredy backup data, how to restore backup of key USB for windows 2008 r2 standard?

    Hello

    The question you posted would be better suited in the TechNet Forums. I suggest you to send your query to the TechNet forum for better support.

    TechNet > Windows Server Forums > management

  • How to view ink levels c4780

    cannot find process to display on photosmart c4780 ink levels

    Hi dondiane,

    I understand that you would like to know how to check ink levels. Follow the steps in this document on the control of the ink levels. It gives you instructions step by step how check ink levels.

    Let me know if this helped.

    Thank you

  • Windows vista 683 gb hard drive full of only the dates, how can I clean it? Fixed thanks

    Windows vista 683 gb hard drive full of only the dates, how can I clean it? I have 9 GB of free space. PC still allows me to watch Netflixs. Cool

    You are welcome.

    See you soon.

  • I received a file winmail DAT - how to open it?

    I received a file winmail DAT - how to open it.

    I do not know.  I do not have a file association for. DAT files (Start / Control Panel / Default programs / associate a file or Protocol with a program / analysis then list for.) (DAT).  I guess you have not either.  Your best bet is to communicate with the person who sent you the file and find out which program was used to create it-then maybe you can get the program or to find a drive for this program.  Good places to watch once you know the program are www.tucows.com and www.download.com.  Here for example is an option for some .dat to open the files that may or may not work with your (given the MANY programs produce and, even once, you need to know what program the file): http://www.winmaildat.com/ (since you say it's a winmail.dat file I hope that this link could actually work).  If it's not, and if you find the name of the program that generated the file, we can help you find something to use to play with - but we need this name (and version number).

    Good luck and I hope this has helped. Lorien - a - MCSE/MCSA/network + / A +.

  • How can I transfer levels pre-defined between (use the gray card) photos in Photoshop elements 14?

    Hello!

    I bought a grey card to be able to define the precise (low temperature) white balance in my Photoshop elements 14 treatment (MS Win 10).

    The first part of the task - the definition of the white balance by a TEST of the gray card shot is very good. However, I can't understand how transfer and apply levels of white balance so determined for the shots on TARGET only container not card gray.

    I guess I need to save/load levels preset in Photoshop elements 14. Coul you please someone teach me how do?

    Kind regards

    Anton

    You have the right version of ACR.

    Presets are not known elements, only Lightroom and Photoshop, you must use other ways.

    Opening several files at the same time: you can select multiple files in the organizer or the Explorer and open them at the same time, they will appear as a film strip vertical on the left side of the ACR dialog box. You can apply a setting in a single file or several or all of the listed files. You can also select all files (Ctrl A) and add all the settings to all files at the same time.

    To apply the "previous conversion", click on the small icon to the right of the title bar 'basic' to unfold a menu drop-down.

  • I am trying to update my creative cloud but am getting an error Code: 3 How do I over come this?

    I am trying to update my creative cloud but am getting an error Code: 3 How do I over come this?

    Hi Heather,

    You can follow the thread: creative cloud update - error code: 3 where this problem is already discussed and resolved.

    You can also follow article: error "Unable to install" received when installing Adobe Creative Cloud desktop application that will help you solve the problem.

    Thank you

    Yann Arora

Maybe you are looking for

  • How can I put a touch in two or more address books without making a 'new contact' for each?

    I want to put several of my contacts in the address book more than one; but, if I move them (drag 'n' drop) a they stay more in the book... I want. I would like to separate the different interest groups and leaders in separate address books, but many

  • Stand-alone labview application (.exe file) does not run correctly

    Hi all I'm doing a labview application only to run on another computer (I never did this before). I went through the tools-> build application prompts and was able to build an exe file. the program is supposed to control a power meter and measures of

  • HP Games program

    HP Pavilion a4327c Desktop, 64 Bit, Windows7 OS. The HP Games icon disappeared in Desktop or Start Menu Programs list.  Program HP Games (WildTangent), figure on the list/menu of Control Panel programs and features. How to restore the icon on the des

  • BlackBerry Smartphones need help reguarding BlackBerry 8530

    I tried to update my BlackBerry Messenger and when I hit the update he gave me a white screen, I took the battery out of the phone it restart because he was stuck on the white screen, once I turned on my phone BlackBerry Messenger back wasn't there,

  • How to uninstall McAfee from my laptop

    I use the computer laptop sony vaio with window7 home premium. I have mcafee antivirus installed with it. I want to uninstall the present. I tried uninstalling software perfect that it goes in the list of programs in the control panel. but it poses p