Identify the hierarchical query paths

Hi all

11 GR 2 DB

create table test_h(parent varchar2(1),child varchar2(1));

insert into test_h values(null,'A');
insert into test_h values('A','B');
insert into test_h values('B','C');
insert into test_h values('C','D');
insert into test_h values('B','E');
insert into test_h values('E','F');
insert into test_h values('','1');
insert into test_h values('1','2');
insert into test_h values('2','3');
commit;

SQL> select *
  2  from test_h;

P C
- -
  A
A B
B C
C D
B E
E F
  1
1 2
2 3

9 rows selected.

SQL> select connect_by_root child root_val,child,level lvl
  2  from test_h
  3  start with parent is null
  4  connect by prior child = parent;

R C        LVL
- - ----------
1 1          1
1 2          2
1 3          3
A A          1
A B          2
A C          3
A D          4
A E          3
A F          4

9 rows selected.

Expected output: An identifier for each different path

R C        LVL  PATH
- - ----------  ----
1 1          1  PATH1
1 2          2  PATH1  
1 3          3  PATH1
A A          1  PATH2
A B          2  PATH2
A C          3  PATH2
A D          4  PATH2
A E          3  PATH3
A F          4  PATH3
Thanks in advance

One-way ticket (not tested... Based on the fact that hierarchical queries will produce output orderly)

SQL> with relation as
  2  (
  3   select connect_by_root child root_val,child,level lvl,rownum rn
  4   from test_h
  5   start with parent is null
  6   connect by prior child = parent
  7  ),
  8  flag as
  9  (
 10   select root_val,child,lvl,rn,
 11          case when lvl = lag(lvl) over(order by rn)+1
 12             then 0
 13              else 1
 14          end flg
 15   from relation
 16  )
 17  select root_val,child,lvl,
 18         'PATH'||sum(flg) over(order by rn) path
 19  from flag;

R C        LVL PATH
- - ---------- --------------------------------------------------
1 1          1 PATH1
1 2          2 PATH1
1 3          3 PATH1
A A          1 PATH2
A B          2 PATH2
A C          3 PATH2
A D          4 PATH2
A E          3 PATH3
A F          4 PATH3

9 rows selected.

Tags: Database

Similar Questions

  • How to convert the hierarchical query of SQL Server (CTE) to Oracle?

    How to convert the hierarchical query of SQL Server (CTE) to Oracle?

    WITH cte (col1, col2) AS
    (
    SELECT col1, col2
    FROM dbo. [tb1]
    WHERE col1 = 12
    UNION ALL
    SELECT c.col1, c.col2
    FROM dbo. [tb1] AS c INNER JOIN cte AS p ON c.col2 = p.col1
    )
    DELETE one
    FROM dbo. [tb1] AS an INNER JOIN b cte
    ON a.col1 = b.col1

    Hello
    Something like this maybe:

    DELETE FROM dbo.tb1 a
     WHERE EXISTS (
      SELECT 1
        FROM dbo.tb1 b
      WHERE a.co11 = b.col1
          AND a.col2 = b.col2
       START WITH b.col1 = 12
      CONNECT BY b.col2 = PRIOR b.col1)
    

    Although you need to do here is to check that CONNECT it BY SELECT, returns records you wait first, then the DELETION should work too.

  • result of the hierarchical query in xml format

    Pls find below the example script table and the desired output format. Output XML must keep the information of hierarchy that is the requirement. Also is it possible to do it in single sql.
    Thank you.
    CREATE TABLE MAIN
    (
    IDENTIFICATION NUMBER,
    NAME VARCHAR2 (20 BYTE)
    );

    INSERT INTO THE HAND (ID, NAME) VALUES)
    1, "OS");
    INSERT INTO THE HAND (ID, NAME) VALUES)
    2, 'Windows');
    INSERT INTO THE HAND (ID, NAME) VALUES)
    3, 'BACK');
    INSERT INTO THE HAND (ID, NAME) VALUES)
    4, "1.x");
    INSERT INTO THE HAND (ID, NAME) VALUES)
    5, "1.1.x");
    INSERT INTO THE HAND (ID, NAME) VALUES)
    7, "1.1.1.1");
    INSERT INTO THE HAND (ID, NAME) VALUES)
    8, ' (1.2');
    INSERT INTO THE HAND (ID, NAME) VALUES)
    9, '2.x');
    INSERT INTO THE HAND (ID, NAME) VALUES)
    10, ' (2.1');
    INSERT INTO THE HAND (ID, NAME) VALUES)
    11, "2.2");
    INSERT INTO THE HAND (ID, NAME) VALUES)
    12, ' 2.2.1');
    INSERT INTO THE HAND (ID, NAME) VALUES)
    13, ' 2.2.2');
    COMMIT;

    CREATE TABLE RELA
    (
    NUMBER OF REL_ID
    IDENTIFICATION NUMBER,
    NUMBER OF PARENT_REL_ID
    );

    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    1, 1, NULL);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    2, 2, 1);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    3, 4, 2);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    4, 5, 3);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    5, 7, 4);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    6, 8, 4);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    7, 9, 2);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    8, 10, 7);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    12, 7, 11);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    14, 9, 9);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    15, 10, 14);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    9, 3, 1);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    10, 4, 9);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    11, 5, 10);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    16, 11, 14);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    13, 8, 10);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    17, 12, 16);
    INSERT INTO RELA (REL_ID, ID, PARENT_REL_ID) VALUES)
    18, 13, 16);
    COMMIT;

    Example query
    Select rel_id, level, lpad (' ', 5 * level) | name,
    XmlElement ("rel_id", XMLAttributes (name as "name", rel_id as "rel_id")) as val
    principal, rela
    where main.id = rela.id
    Start with parent_rel_id is null
    Connect prior rel_id = parent_rel_id;

    Required xml output
    Required xml output

    < 1 name = "OS" >
    < name 2 = "Windows" >
    < 3 name = "1.x" 3 > ""
    < name 4 = "1.1.x" >
    < name 5 = "1.1.1.1" > < / 5 >
    < name 6 = "1.2" > < / 6 >
    < / 4 >
    < / 3 >
    < name = "" 2.x 7 ">"
    < name 8 = "2.1" > < / 8 >
    < / 7 >
    < / 2 >
    < name 9 'BACK' = >
    < name = "2.x 14" > "
    < name 15 = "2.1" > < / 15 >
    < name 16 = "2.2" >
    < name 17 = '2.2.1' > < / 17 >
    < name 18 = '2.2.2' > < / 18 >
    < / 16 >
    < / 14 >
    < 10 name = "1.x" > "
    < name 11 = "1.1.x" >
    < name 12 = "1.1.1.1" > < / 12 >
    < / 11 >
    < name 13 = "1.2" >
    < / 13 >
    < / 10 >
    < / 9 >
    < 1 >

    Published by: delights on September 18, 2009 16:47

    Published by: delights Sep 19, 2009 09:53

    No string function helps eliminate r because the xml output is great (in a real scenario) which gives "buffer too small" error.

    Try sth like

    SQL>select replace(x.column_value.getclobval(), 'XXX') xml
      2    from xmltable ('.' passing dbms_xmlgen.getxmltype (dbms_xmlgen.
      3                   newcontextfromhierarchy (
      4                   'select level, xmlelement (evalname ''XXX'' || rel_id, xmlattributes(name "name")) name
      5        from main, rela
      6       where main.id = rela.id
      7  start with parent_rel_id is null
      8  connect by prior rel_id = parent_rel_id'
      9                   ))) x
     10  /
    
    XML
    --------------------------------------------------------------------------------
    
    <1 name="OS">
      <2 name="Windows">
        <3 name="1.x">
          <4 name="1.1.x">
            <5 name="1.1.1.1"/>
            <6 name="1.2"/>
          
        
        <7 name="2.x">
          <8 name="2.1"/>
        
      
      <9 name="DOS">
        <10 name="1.x">
          <11 name="1.1.x">
            <12 name="1.1.1.1"/>
          
          <13 name="1.2"/>
        
        <14 name="2.x">
          <15 name="2.1"/>
          <16 name="2.2">
            <17 name="2.2.1"/>
            <18 name="2.2.2"/>
          
        
      
    
    
    SQL>
    

    Newcontextfromhierarchy is not a documented feature do not know if it will issue in releated support this in production code.

    Oracle's recommeds to use newcontextfromhierarchy in Metalink Note 882887.1 ;)

  • To find the child sheet most to the hierarchical query

    Hello
    I need to findout the child sheet plus for parent to my request.
    create table test_Test1 ( ID , PRNT_ID ,CHLD_ID)
     as 
     (select 1 , 10 ,20 from dual union all
     select 2 , 10 ,21 from dual union all
     select 3 , 20 ,30 from dual union all
     select 4 , 20 ,31 from dual union all
     select 5 , 30 ,40 from dual union all
     select 6 , 31 ,41 from dual union all
     select 7 , 100 ,200 from dual union all
     select 8 , 100 ,210 from dual union all
     select 9 , 200 ,300 from dual union all
     select 10 , 200 ,310 from dual union all
     select 11 , 300 ,400 from dual union all
     select 12 , 310 ,410 from dual )
    Expected results
    PRNT_ID   CHLD_ID
    ------    ------
    10            21
    10            40
    10            41
    100            210
    100            400
    100            410
    Thank you

    What:

    SQL> select * from t;
    
            ID    PRNT_ID    CHLD_ID
    ---------- ---------- ----------
             1         10         20
             2         10         21
             3         20         30
             4         20         31
             5         30         40
             6         31         41
             7        100        200
             8        100        210
             9        200        300
            10        200        310
            11        300        400
            12        310        410
    
    12 rows selected.
    
    SQL> select min(isroot)
      2  ,      chld_id
      3  from ( select connect_by_root prnt_id  isroot
      4         ,      chld_id
      5         ,      connect_by_isleaf isleaf
      6         from   t
      7         connect by prnt_id = prior chld_id
      8       )
      9  where isleaf = 1
     10  group by chld_id;
    
    MIN(ISROOT)    CHLD_ID
    ----------- ----------
             10         21
             10         40
             10         41
            100        210
            100        400
            100        410
    
    6 rows selected.
    
  • hierarchical query of VO in the ofa page

    Hello

    I try to use the hierarchical query in VO for the display of all levels of supervisors for employee and want to display in populist:

    This is the query

    REPLACE SELECT distinct (mgx.full_name, "',' ') supervisor_full_name;

    XPP. Employee_number

    OF per_assignments_x pax,.

    per_people_x ppx,

    per_people_x mgx,

    per_positions pp,

    per_jobs pj,

    per_position_definitions ppd

    WHERE ppx.person_id = pax.person_id

    AND ppx.current_employee_flag = 'Y '.

    AND mgx.person_id = pax.supervisor_id

    AND pp.position_id = pax.position_id

    AND pj.job_id = pax.job_id

    AND ppd.position_definition_id = pp.position_definition_id

    START WITH ppx.employee_number =: 1

    CONNECT BY PRIOR MGX.employee_number = ppx.employee_number AND LEVEL < 6

    I'm trying to get this VO implemented PR when I run the page in jdeveloper with parameter binding, it's getting error.

    oracle.apps.fnd.framework.OAException: oracle.jbo.SQLStmtException: 27122 Houston: SQL error in the preparation of the statement.

    I would like to delete the parameter binding and executing CO VO past these two statements may be:

    START WITH ppx.employee_number =: 1

    CONNECT BY PRIOR MGX.employee_number = ppx.employee_number AND LEVEL < 6

    How can I do?

    Thank you

    MK

    has worked?

  • Reg: Hierarchical query (using connection by)

    Hi all
    I got the result with the hierarchical query in the form:
    * / qxxh *.
    * / qxxh/jxobcbg *.
    * / qxxh/jxobcbg/n00wcp4 *.
    * / qxxh/jxobcbg/n00wcp4 / 000263 x *.
    * / qxxh/jxobcbg/n00wcp4 / x 000263 / p0263 *.
    * / qxxh/jxxocbg *.
    * / qxxh/jxxocbg/n00voc1 *.
    * / qxxh/jxxocbg/n00voc1 / x 000589 *.
    * / qxxh/jxxocbg/n00voc1 / x 000589 / p0589 *.
    * / qxxh/jxuwxxh *.
    * / qxxh/jxuwxxh/n00xpxf *.
    * / qxxh, jxuwxxh, n00xpxf, m00bxpl *.
    * / qxxh/jxuwxxh/n00xpxf/m00bxpl / 000522 x *.
    * / qxxh/jxuwxxh/n00xpxf/m00bxpl / 000522 x / p0522 *.

    Here, I want to select only the maximum path. Here I used "SYS_CONNECT_BY_PATH.
    Please let meknow how to do this?
    Thanks in advance.

    Published by: udeffcv on December 9, 2009 22:03

    udeffcv wrote:
    Hi all
    I got the result with the hierarchical query in the form:
    * / qxxh *.
    * / qxxh/jxobcbg *.
    * / qxxh/jxobcbg/n00wcp4 *.
    * / qxxh/jxobcbg/n00wcp4 / 000263 x *.
    * / qxxh/jxobcbg/n00wcp4 / x 000263 / p0263 *.
    * / qxxh/jxxocbg *.
    * / qxxh/jxxocbg/n00voc1 *.
    * / qxxh/jxxocbg/n00voc1 / x 000589 *.
    * / qxxh/jxxocbg/n00voc1 / x 000589 / p0589 *.
    * / qxxh/jxuwxxh *.
    * / qxxh/jxuwxxh/n00xpxf *.
    * / qxxh, jxuwxxh, n00xpxf, m00bxpl *.
    * / qxxh/jxuwxxh/n00xpxf/m00bxpl / 000522 x *.
    * / qxxh/jxuwxxh/n00xpxf/m00bxpl / 000522 x / p0522 *.

    Here, I want to select only the maximum path. Here I used "SYS_CONNECT_BY_PATH.
    Please let meknow how to do this?
    Thanks in advance.

    Published by: udeffcv on December 9, 2009 22:03

    What do you mean by maximum path? is this...
    * / qxxh/jxobcbg/n00wcp4 / x 000263 / p0263 *.
    * / qxxh/jxxocbg/n00voc1 / x 000589 / p0589 *.
    * / qxxh/jxuwxxh/n00xpxf/m00bxpl / 000522 x / p0522 *.

    is it child nodes?
    so, you would like to see
    Column nickname... CONNECT_BY_ISLEAF example, you can find it in the link below
    http://download.Oracle.com/docs/CD/B14117_01/server.101/b10759/pseudocolumns001.htm#sthref670

    Ravi Kumar

  • Help with hierarchical query

    I'm trying to parse each string inside of individual characters. For this, I used the link by the hierarchical query clause. I am not able to use connect by correctly. Here is my example.

    with the CBC as)

    Select 1: the nurse, 'abc' double union all Str

    Select 2: the nurse, '123' Str of all union double

    Select 3: the nurse, "pqr xyz" dual union all Str

    Select option 4: the nurse, 'john doe' double Str

    )

    Select the level, homobasidiomycetes, substr(s.str,level,1) as chr

    s of the CBC

    connect nocycle level < = length (homobasidiomycetes)

    /

    The above query returns 3 028 lines when I want that 21. Adding a distinct to select, it's what I want.

    with the CBC as)

    Select 1: the nurse, 'abc' double union all Str

    Select 2: the nurse, '123' Str of all union double

    Select 3: the nurse, "pqr xyz" dual union all Str

    Select option 4: the nurse, 'john doe' double Str

    )

    Select distinct level, homobasidiomycetes, substr(s.str,level,1) as chr

    s of the CBC

    connect nocycle level < = length (homobasidiomycetes)

    order by str, level

    /

    For a large data set I could end up with several million rows before the separate would lead. So, how can I restrict the connection by clause to go within each line.

    Assuming that the AI is the primary key, you could do this:

    with src as (
      select 1 as rn, 'abc' as str from dual union all
      select 2 as rn, '123' as str from dual union all
      select 3 as rn, 'pqr xyz' as str from dual union all
      select 4 as rn, 'john doe' as str from dual
    )
    select level,
          s.str,
          substr(s.str,level,1) as chr
    from src s
    connect by level <= length(s.str)
              and prior rn = rn
              and prior dbms_random.value is not null;
    

    See DBMS_RANDOM.value in a hierarchical solution for string to table? for an exchange of views around the case.

  • How to measure the performance of the sql query?

    Hi Experts,

    How to measure the cost of performance, efficiency and CPU of an sql query?

    What are all the measures available to a sql query?

    How to identify the optimal query writing?

    I use Oracle 9i...

    It'll be useful for me to write the effective query...

    Thanks and greetings

    PSRAM wrote:
    Could you tell me how to activate the PLUSTRACE role?

    First put on when you do a search on PLUSTRACE: http://forums.oracle.com/forums/search.jspa?threadID=&q=plustrace&objID=f75&dateRange=all&numResults=15&rankBy=10001

    Kind regards
    Rob.

  • 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 avoid the report query needs a unique key to identify each line

    Hello

    How to avoid the error below

    The report query needs a unique key to identify each row. The supplied key cannot be used for this query. Please change the report attributes to define a unique key column.

    I have master-detail tables but without constraints, when I created a query as dept, emp table he gave me above the error.

    Select d.deptno, e.ename

    by d, e emp dept

    where e.deptno = d.deptno

    Thank you and best regards,

    Ashish

    Hi mickael,.

    Work on interactive report?

    You must set the column link (in the attributes report) to something other than "link to display single line." You can set it to 'Exclude the column link' or 'target link to Custom.

  • Fill out the "limits" for nodes in a hierarchical query

    Hello

    I have a table called V that is organized hierarchically. You can have a "hierarchical" view by issuing the following query (PARENT_ID NULL is the root):

    SELECT rn, 
           LPAD ('_ ', 2 * (LEVEL - 1), '_ ') || id tree,
           id, 
           parent_id, 
           lvl, 
           cbi, 
           tree_lft, 
           tree_rgt, 
           tl, 
           tr 
    FROM v
    START WITH parent_id IS NULL 
    CONNECT BY PRIOR id = parent_id
    ORDER SIBLINGS BY rn;
    

    Now, my task is to fill in the TL and TR "limits" that define the beginning and end of each group under all nodes.

    For a better understanding, I've already placed the expected results in the TREE_LFT and TREE_RGT columns. The hierarchy follows the order of the columns, RN.

    TREE_LFT begins with the root (RN = 1) line 1 and lights up gradually until it reaches the leaves (CBI = 1). If a sheet is found, TREE_RGT is set to + 1 TREE_LFT.

    Then, if another sheet follows (online RN = 4), it takes the next increment TREE_LFT + 1. And so on. When all the leaves under the node assigned a value for TREE_LFT and TREE_RGT,

    the TREE_RGT value for the node takes the last value assigned to its + 1 children. For example, you can see that the children of the node id = 14 come from RN = RN = 30 3. The value of TREE_RGT

    for the last child (RN = 30) is 58, so the TREE_RGT value for the node id = 14 will be 58 + 1 = 59.

    Then the numbering continues gradually for the rest of the hierarchy. There is a new node to RN = 31, so TREE_LFT starts from TREE_RGT of the previous node + 1, that is, 60.

    This logic continues until the end of the hierarchy following RN.

    Using a STANDARD clause, I've managed to fill TL (you can see TL = TREE_LFT) and TR for the leaves.

    I also managed to complete the TR for nonleaf with a second MODEL clause, but it runs very bad when the array is much more filled than that.

    This is the STANDARD clause that allows you to calculate the TR:

    select *
    from v
    model 
    dimension by (id, parent_id, cbi)
    measures (rn, tree_lft, tree_rgt, lvl, tl, tr)
    rules (
           tr[ANY, ANY, 0] order by lvl desc, id, parent_id = max(tr)[ANY, CV(id), ANY] + 1
          )
          ;
    

    It does the job as expected, but the generic as well as ordered rule is a killer when it is applied to a larger painting.

    So my question is, is there a way I can write a query that would do the same as this one, but with better performance (functions analytical pattern)?

    I know that I can not really clear that it is not easy to explain.

    I use a database with 11.2.0.3 Enterprise Edition.

    Thank you

    Here are the scripts to test:

    create table v (RN NUMBER, NUMBER identification, PARENT_ID NUMBER, TREE_LFT NUMBER, TREE_RGT NUMBER, CBI NUMBER, NUMBER of LVL, TL NUMBER, NUMBER of TR);

    insert into v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) values (1, 3, null, 1, 100, 0, 1, 1, null);

    insert into v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) values (2, 14, 3, 2, 59, 0, 2, 2, null);

    insert into v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) values (3, 224, 14, 3, 4, 1, 3, 3, 4);

    insert into v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) values (4, 221, 14, 5, 6, 1, 3, 5, 6);

    insert into v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) values (5, 236, 14, 7, 8, 1, 3, 7, 8);

    insert into v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) values (6, 218, 14, 9, 10, 1, 3, 9, 10);

    insert into v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) values (7, 230, 14, 11, 12, 1, 3, 11, 12);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (8, 234, 14, 13, 14, 1, 3, 13, 14);

    insert into v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) values (9, 235, 14, 15, 16, 1, 3, 15, 16);

    insert into v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) values (10, 253, 14, 17, 18, 1, 3, 17, 18);

    insert into v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) values (11, 245, 14, 19, 20, 1, 3, 19, 20);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (12, 231, 14, 21, 22, 1, 3, 21, 22);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (13, 228, 14, 23, 24, 1, 3, 23, 24);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (14, 243, 14, 25, 26, 1, 3, 25, 26);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (15, 219, 14, 27, 28, 1, 3, 27, 28);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (16, 220, 14, 29, 30, 1, 3, 29, 30);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (17, 229, 14, 31, 32, 1, 3, 31, 32);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (18, 248, 14, 33, 34, 1, 3, 33, 34);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (19, 244, 14, 35, 36, 1, 3, 35, 36);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (20, 254, 14, 37, 38, 1, 3, 37, 38).

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (21, 247, 14, 39, 40, 1, 3, 39, 40);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (22, 225, 14, 41, 42, 1, 3, 41, 42);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (23, 226, 14, 43, 44, 1, 3, 43, 44).

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (24, 249, 14, 45, 46, 1, 3, 45, 46);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (25, 250, 14, 47, 48, 1, 3, 47, 48);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (26, 252, 14, 49, 50, 1, 3, 49, 50);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (27, 251, 14, 51, 52, 1, 3, 51, 52);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (28, 246, 14, 53, 54, 1, 3, 53, 54);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (29, 256, 14, 55, 56, 1, 3, 55, 56);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (30, 255, 14, 57, 58, 1, 3, 57, 58);

    insert into v values (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (31, 15, 3, 60, 75, 0, 2, 60, null);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (32, 222, 15, 61, 62, 1, 3, 61, 62);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (33, 223, 15, 63, 64, 1, 3, 63, 64);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (34, 241, 15, 65, 66, 1, 3, 65, 66);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (35, 242, 15, 67, 68, 1, 3, 67, 68);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (36, 227, 15, 69, 70, 1, 3, 69, 70);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (37, 213, 15, 71, 72, 1, 3, 71, 72);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (38, 214, 15, 73, 74, 1, 3, 73, 74);

    insert into v values (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (39, 16, 3, 76, 81, 0, 2, 76, null);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (40, 238, 16, 77, 78, 1, 3, 77, 78);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (41 237, 16, 79, 80, 1, 3, 79, 80);

    insert into v values (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (42, 17, 3, 82, 85, 0, 2, 82, null);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (43, 239, 17, 83, 84, 1, 3, 83, 84);

    insert into v values (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (44, 18, 3, 86, 99, 0, 2, 86, null);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (45, 232, 18, 87, 88, 1, 3, 87, 88);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (46, 233, 18, 89, 90, 1, 3, 89, 90);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (47, 215, 18, 91, 92, 1, 3, 91, 92);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (48, 216, 18, 93, 94, 1, 3, 93, 94);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (49, 217, 18, 95, 96, 1, 3, 95, 96);

    insert into values of v (RN, ID, PARENT_ID, TREE_LFT, TREE_RGT, CBI, LVL, TL, TR) (50, 240, 18, 97, 98, 1, 3, 97, 98);

    Hi Greg,.

    It looks like you use the defined nested data model.

    See solutions mentioned above:

    https://community.Oracle.com/thread/2603314 (last post)

    https://community.Oracle.com/message/12468999#12468999

  • Summarize the costs with hierarchical query

    Hello Oracle experts! I need some advice because I am very new to the hierarchical queries really new to Oracle. I have the following table:

    CREATE TABLE routes

    (

         from  VARCHAR2(15),

         to  VARCHAR2(15),

         cost NUMBER

    );

    INSERT INTO routes VALUES('San Francisco', 'Denver', 1000);

    INSERT INTO routes VALUES('San Francisco', 'Dallas', 10000);

    INSERT INTO routes VALUES('Denver', 'Dallas', 500);

    INSERT INTO routes VALUES('Denver', 'Chicago', 2000);

    INSERT INTO routes VALUES('Dallas', 'Chicago', 600);

    INSERT INTO routes VALUES('Dallas', 'New York', 2000);

    INSERT INTO routes VALUES('Chicago', 'New York', 3000);

    INSERT INTO routes VALUES('Chicago', 'Denver', 2000);

    I want to calculate the costs through the hierarchy, to get the following result:

    FROM            TO             COST

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

    San Francisco Dallas   10000   //San Francisco -> Dallas

    San Francisco Denver 1000    //San Francisco -> Denver

    San Francisco Chicago   10600   //San Francisco -> Dallas -> Chicago (10000 + 600)

    San Francisco New York   12000   //San Francisco -> Dallas -> New York (10000 + 200)

    San Francisco Chicago   3000    //San Francisco -> Denver -> Chicago (1000 + 2000)

    San Francisco Dallas   1500    //San Francisco -> Denver -> Dallas (1000 + 500)

    etc..

    I from imagined using the CONNECT BY PRIOR statement to get the hierarchy and have written a query that runs through him:

    SELECT CONNECT_BY_ROOT from, to

    FROM routes

      CONNECT BY NOCYCLE PRIOR to = from;

    But I have absolutely no idea how summarize right costs, I could use some help.

    Thank you for your advice.

    Hello

    Here's a way

    SELECT DISTINCT

    CONNECT_BY_ROOT from_city AS from_city

    to_city

    , SYS_CONNECT_BY_PATH (to_city, '->') is ARRESTED - if wanted

    , XMLQUERY (SYS_CONNECT_BY_PATH (cost, '+'))

    BACK CONTENT

    ) .getnumberval () SUCH as total_cost

    CHANNELS

    WHERE CONNECT_BY_ROOT from_city <> to_city

    CONNECT BY NOCYCLE from_city = to_city PRIOR

    ORDER BY from_city

    to_city

    stops

    ;

    FROM and TO are the keywords of the Oracle, they really ugly column names.  I have changed the from_city and to_city.

  • Subselect query returns "invalid identifier", but the nested query return lines

    I don't think it's a general SQL question.

    Select * from persons where person_id in)

    Select person_id with people whose name = 'Obama' - subquery

    ) and age > 18;

    When I run the subquery, I get:

    ORA-00904: "PERSON_ID": invalid identifier

    00904, 00000 - '% s: invalid identifier '.

    * Cause:

    * Action:

    Error on line: column 5: 8

    This is because the table people do not have the person_id field.

    But when I run the nested together query it returns all the lines in people with the AGE greater than 18.

    How is he succeeds when the subquery is obviously wrong?

    363f652b-263D-4418-933F-74a1d0a41b4c wrote:

    I don't think it's a general SQL question.

    Select * from persons where person_id in)

    Select person_id with people whose name = 'Obama' - subquery

    ) and age > 18;

    When I run the subquery, I get:

    ORA-00904: "PERSON_ID": invalid identifier

    00904, 00000 - '% s: invalid identifier '.

    * Cause:

    * Action:

    Error on line: column 5: 8

    This is because the table people do not have the person_id field.

    But when I run the nested together query it returns all the lines in people with the AGE greater than 18.

    How is he succeeds when the subquery is obviously wrong?

    Yes - this is a general SQL question and ask often enough.

    Correlated subqueries depend on the inner query, be able to see and access to the columns of the outer query. Normally see you referenced in the WHERE clause of the subquery and not in the SELECT clause, but the reference is valid in both places. This works because the columns of the tables in the main query are accessible in the subquery. "Person_id" is probably a column in the table 'people '.

    Which can be a cause of problems 'odd' when the column (in your case "person_id") is more of a table.

    Use an alias in the subquery in the subquery and you will find that it will not succeed.

    See these two articles AskTom where he addresses this specific issue

    http://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:3317493900346468494

    http://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:155200640564

  • Getting the line without the use of hierarchical query values

    Hi all


    I want to know the hierarchical values without using a hierarchical query. I have two tables EMP, DEPT

    EMP table is to have two columns (empid, mgrid)
    DEPT table is to have two columns (deptid, empid)

    Data of the EMP

    1,
    2, 1
    3, 2
    4, 3

    Data DEPT

    10, 1

    Each time, I gave deptid = 10, I need to know the this deptid empid then who is this empid (child levels as well). In this case, the output should be
    1
    2
    3
    4

    I don't want to use hierarchical query.

    Thanks in advance.


    Thank you
    PAL

    You can use the RECURSIVE subquery, if you are 11 GR 2, like this

    SQL> with EMP (empid,mgrid) as
      2  (
      3  select 1,null from dual union all
      4  select 2, 1 from dual union all
      5  select 3, 2 from dual union all
      6  select 4, 3 from dual
      7  ),
      8  dept(deptid,empid) as
      9  (
     10  select 10, 1  from dual
     11  ),
     12  t(empid,mgrid) as
     13  (
     14  select empid,mgrid
     15  from emp e
     16  where empid in (
     17      select d.empid
     18      from dept d
     19      where deptid = 10
     20                  )
     21  union all
     22  select e.empid,e.mgrid
     23  from emp e, t
     24  where e.mgrid = t.empid
     25  )
     26  select *
     27  from t;
    
         EMPID      MGRID
    ---------- ----------
             1
             2          1
             3          2
             4          3
    

    Yet, the question is valid - why can't use you a hierarchical query?

    Published by: JAC on May 29, 2013 12:37

  • The recursive subquery factoring vs hierarchical query

    Experts,

    Here it is two queries, I'm executing using hierarchical of recursive subquery and new classical approach. Problem came out was different for the recursive subquery factoring, as this is not displayed parent-child approach, while forwards connect shows parent-child mode. Query 1, I use hierarchical as show good output parent-child, is approaching the 2 query displays all of the nodes of level 1 and then level 2 nodes. I want the query 2 output to be the same as query 1. change query 2 as required.

    Note: the output of the two queries post as in sqlplus and toad. Please copy and use in your command prompt.

    QUERY 1:

    with the hand in the form (select 1 id, name 'John', null, mgrid Union double all the)
    Select 2 id, the name of 'michael', null, mgrid Union double all the
    Select 3 id, the name of "peter", null, mgrid Union double all the
    Select 4 id, the name of "henry", 1 mgrid Union double all the
    Select 5 id, "nickname", mgrid 2 Union double all the
    Select 6 id, "pao" name, mgrid 3 of all the double union
    Select 7 id, the name of 'kumar', mgrid 3 of all the double union
    Select 8 id, the name 'parker', mgrid 3 of all the double union
    Select 9 id, the name of "mike", 5 double mgrid),
    Select lpad (' ', 2 *(level-1)). name, key start with mgrid level is null connect by prior id = mgrid.

    OUTPUT:

    LEVEL NAME
    ------------------------------ ----------
    John 1
    Henry 2
    Michael 1
    Nick 2
    Mike 3
    Stone 1
    PAO 2
    Kumar 2
    Parker 2

    9 selected lines.

    QUERY 2:

    with the hand in the form (select 1 id, name 'John', null, mgrid Union double all the)
    Select 2 id, the name of 'michael', null, mgrid Union double all the
    Select 3 id, the name of "peter", null, mgrid Union double all the
    Select 4 id, the name of "henry", 1 mgrid Union double all the
    Select 5 id, "nickname", mgrid 2 Union double all the
    Select 6 id, "pao" name, mgrid 3 of all the double union
    Select 7 id, the name of 'kumar', mgrid 3 of all the double union
    Select 8 id, the name 'parker', mgrid 3 of all the double union
    Select 9 id, the name of "mike", 5 double mgrid),
    / * Select lpad (' ', 2 *(level-1)). name, key start with mgrid level is null connect by prior id = mgrid. * /
    secmain (id, name, mgrid, hierlevel) as (select id, name, mgrid, 1 hierlevel of the main where mgrid is null
    Union of all the
    Select m.id, $m.name, m.mgrid, sm.hierlevel + 1 in m main join secmain sm on(m.mgrid=sm.id))
    cycle is_cycle set id 1 default 0
    Select lpad (' ', 2 *(hierlevel-1)). name, secmain hierlevel.

    OUTPUT:

    NAME HIERLEVEL
    ------------------------------ ----------
    John 1
    Michael 1
    Stone 1
    Henry 2
    Nick 2
    Parker 2
    Kumar 2
    PAO 2
    Mike 3

    9 selected lines.

    For example

    SQL> with main as(select 1 id,'john' name,null mgrid from dual union all
      2  select 2 id,'michael' name,null mgrid from dual union all
      3  select 3 id,'peter' name,null mgrid from dual union all
      4  select 4 id,'henry' name,1 mgrid from dual union all
      5  select 5 id,'nick' name,2 mgrid from dual union all
      6  select 6 id,'pao' name,3 mgrid from dual union all
      7  select 7 id,'kumar' name,3 mgrid from dual union all
      8  select 8 id,'parker' name,3 mgrid from dual union all
      9  select 9 id,'mike' name,5 mgrid from dual),
     10  secmain (id,name,mgrid,hierlevel) as
     11  (select id,name,mgrid,1 hierlevel from main
     12   where mgrid is null
     13   union all
     14   select m.id,m.name,m.mgrid,sm.hierlevel+1 from main m join secmain sm on(m.mgrid=sm.id))
     15  search depth first by name set seq
     16  cycle id set is_cycle to 1 default 0
     17  select lpad(' ',2*(hierlevel-1))||name name,hierlevel from secmain order by seq;
    
    NAME        HIERLEVEL
    ---------- ----------
    john                1
      henry             2
    michael             1
      nick              2
        mike            3
    peter               1
      kumar             2
      pao               2
      parker            2
    
    9 rows selected.
    
    SQL> 
    

    Published by: Dom Brooks on November 23, 2011 13:52
    Edited for the scope of the search and detection of cycle

Maybe you are looking for