Hierarchical query help need to Oracle

Hello
I'm in a deep trouble.

I want to do a hierarchical query in my TreeView table in oracle. But I want that the query retrieves information path between two nodes.

Review the table below.

------------------------------------------------------
Node ID | Name of the node. ID of the parent
-----------------------------------------------------
1A 0
2 1
3C0
4 2
-----------------------------------------------------

What I want is that I'll give you 2 node id in the request. One is the source node and the other is the destination node.
so if I put 1 node and node 4, I want to be the output

1-> 2-> 4

I want just a return line. Or there may be several rows but stands only the node of this path.

1
2
4
----

This means that I don't want to node 3 in the search result.


More if I have the source as 4 node and the node of destination as 1 then I will also get an output of the query as below-
4-> 2-> 1

This means that the search for the query for the result in a way directional bi.


If anyone can give me some information about who it's going to be great for me.

Published by: user13276471 on December 31, 2012 03:21

Hello

Welcome to the forum!

Assuming you have 2 separate nodes: Node_A and: node_b, you can find the hierarchy from one to the other like this:

SELECT     SYS_CONNECT_BY_PATH (node_id, ',')     AS lineage
FROM     table_x
WHERE     node_id     IN (:node_a, :node_b)
AND     LEVEL     > 1
START WITH     node_id          IN (:node_a, :node_b)
CONNECT BY     parent_id     = PRIOR node_id
;

It will work if: the Node_A is an ancestor of the: node_b, or if: node_b is an ancestor of the: Node_A.
If is the ancestor of the other, then it will produce 0 rows.

Tags: Database

Similar Questions

  • Hierarchical query help

    Version: Oracle Database 11 g Enterprise Edition Release 11.2.0.2.0 - 64 bit Production

    Hello

    I can't just writing this hierarchical query.

    CREATE TABLE resources
    (
        resource_name   VARCHAR2( 20 )
       ,resource_id     NUMBER
       ,resource_mgr_id NUMBER
    );
    
    INSERT INTO resources(resource_name,resource_id,resource_mgr_id)
    VALUES ('Phupinder',29269,29298);
    
    INSERT INTO resources(resource_name,resource_id,resource_mgr_id)
    VALUES ('Terry',29298,NULL);
    
    INSERT INTO resources(resource_name,resource_id,resource_mgr_id)
    VALUES ('Matt',4876,32942);
    
    INSERT INTO resources(resource_name,resource_id,resource_mgr_id)
    VALUES ('Bryan',29429,29269);
    
    INSERT INTO resources(resource_name,resource_id,resource_mgr_id)
    VALUES ('Cathy',32942,4922);
    
    INSERT INTO resources(resource_name,resource_id,resource_mgr_id)
    VALUES ('Scott',4257,29429);
    
    INSERT INTO resources(resource_name,resource_id,resource_mgr_id)
    VALUES ('Joe',44026,4876);
    
    INSERT INTO resources(resource_name,resource_id,resource_mgr_id)
    VALUES ('Greg',4922,4257);
    
    
    
    
    
    
    
    
    

    I also tried this:

    1. SELECT DISTINCT R.resource_name
    2. r.resource_id
    3. From r resources
    4. WHERE r.resource_id IN (SELECT DISTINCT r2.resource_mgr_id
    5. RESOURCE r2
    6. WHERE r.resource_id = r2.resource_mgr_id OR
    7. R2.resource_mgr_id IS NULL)
    8. ORDER BY 1;

    Which gives correct results, but isn't that a hierarchical query?

    It was the (almost) good solution to your problem.

    The only thing is:

    -you don't need to do a correlated subquery to solve the problem

    -you don't need to separate your subqurey

    -you don't need to separate your mainquery

    SELECT resource_name, id_ressource

    RESOURCES

    where id_ressource in (select r2.resource_mgr_id from r2 resources);

    would be sufficient.

    HTH

  • A hierarchical query help

    I have two tables:

    The FOLDERS where the areas concerned are: folderId, parentFolderId, folderQuota
    and DOCUMENTS where the areas concerned are: folderId, size

    FILES is hierarchical - parentFolderId of the file of the child has the folderId of the parent value
    folderQuota the value null (undefined quota)


    Now, I need to run a query with the following recursive logic

    < i > function calcQuota (folderIdpar, isFirstpar) {}

    If (not isFirstpar) and (folderQuota of the folder with folderIdpar is not null) return folderQuota;
    Returns the size of all documents where folderId = folderIdpar + calcQuota (folderId of all children, false);

    } < /i >

    (I hope the pseudo-code is understandable - the query is executed as < i > calcQuota (originalFolderId, true) < /i >).

    Now, my question is if I can accomplish this with a single hierarchical query, or if I should implement as a recursive procedure stored.

    Thank you!

    P.S. I use Oracle XE (10g)

    Hello

    If you want to ignore the quotas at the level = 1:

    WITH     tree     AS
    (
         SELECT     folder_id
         ,     CASE
                   WHEN  LEVEL > 1
                   THEN  quota
              END                         AS effective_quota
         FROM     folders          f
         START WITH     folder_id          = :start_folder_id
         CONNECT BY     parent_folder_id     = PRIOR folder_id
              AND     (   PRIOR quota          IS NULL
                   OR  LEVEL          = 2
                   )
    )
    SELECT     :start_folder_id
    ,     SUM ( NVL ( t.effective_quota
                , d.document_size
                )
             )          AS total_size
    FROM     tree               t
    LEFT OUTER JOIN documents     d     ON     t.folder_id          = d.folder_id
                             AND     t.effective_quota     IS NULL
    ;
    
  • Hierarchical Oracle query help needed - path between the crux of two brothers and sisters

    I want to find the path between two nodes of oracle hierarchical Table.

    Consider the following case-
    NodeId - ParentId
    =============
    1 > > > > > > 0
    2 > > > > > > 1
    3 > > > > > > 2
    4 > > > > > > 3
    5 > > > > > > 0
    6 > > > > > > 5
    Here I want to query the database table that if there is a path between nodes 3 and 5?
    The previous query you provided work upwards to the root node.

    Here is my expected result, 3-> 2-> 1-> 0-> 5

    Yet once if I have a query in the table to get the path between 1 and 3, I want to get out of the way - next
    1-> 2-> 3

    Therefore, the query works in both cases. Where ADI root can act as an intermediate or no node.

    Can you please guide me how I can get it?

    Thank you.

    Hello

    user13276471 wrote:
    I want to find the path between two nodes of oracle hierarchical Table.

    Consider the following case-
    NodeId - ParentId
    =============
    1 >>>>>> 0
    2 >>>>>> 1
    3 >>>>>> 2
    4 >>>>>> 3
    5 >>>>>> 0
    6 >>>>>> 5
    Here I want to query the database table that if there is a path between nodes 3 and 5?
    The previous query

    What application is this? If you're referering to another thread, then post a link, such as {message identifier: = 10769125}

    you provided work upwards to the root node.

    Here is my expected result, 3--> 2--> 1--> 0--> 5

    Yet once if I have a query in the table to get the path between 1 and 3, I want to get out of the way - next
    1--> 2--> 3

    Therefore, the query works in both cases. Where ADI root can act as an intermediate or no node.

    Can you please guide me how I can get it?

    I think you want something like this:

    WITH     bottom_up_from_src    AS
    (
         SELECT     nodeid
         ,     parentid
         FROM     table_x
         START WITH     nodeid      = :src_nodeid
         CONNECT BY     nodeid   = PRIOR parentid
    )
    ,     bottom_up_from_dst     AS
    (
         SELECT     *
         FROM     bottom_up_from_src
        UNION ALL
         SELECT     parentid     AS nodeid
         ,     nodeid          AS parentid
         FROM     table_x
         WHERE     nodeid     NOT IN (
                                          SELECT  nodeid
                                   FROM    bottom_up_from_src
                                      )
         START WITH     nodeid        = :dst_nodeid
         CONNECT BY     nodeid        = PRIOR parentid
    )
    SELECT      :src_nodeid || SYS_CONNECT_BY_PATH (parentid, '-->')     AS display_path
    FROM       bottom_up_from_dst
    WHERE       parentid     = :dst_nodeid
    START WITH  nodeid     = :src_nodeid
    CONNECT BY  nodeid     = PRIOR parentid
    ;
    

    This will show how you can get it from: src_nodeid at dst_nodeid, moving to the top or to the bottom of a hierarchy at a time step. This will work regardless of the fact that


    • : src_nodeid is the ancestor of the: dst_nodeid, or
    • : src_nodeid is a descendant of: dst_nodeid, or
    • both: src_nodeid and: dst_nodeid are the descendants of another node (e.g. 0).

    I hope that answers your question.
    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) and also publish outcomes from these data.
    Explain, using specific examples, how you get these results from these data.
    Always say what version of Oracle you are using (for example, 11.2.0.2.0). It is always important, but particularly so with CONNECT BY queries, because each version since Oracle 7 had significant improvements in this area.
    See the FAQ forum {message identifier: = 9360002}

  • XML QUERY HELP NEEDED

    Hello

    Need help with writing a query.
    SQL> SELECT xmlelement("P",xmlforest(P.process_id AS Ppid),
      2   xmlagg(xmlelement("PI",XMLFOREST( PI.question_id AS PIqid,
      3   PI.process_id AS PIpid,
      4   PI.innertext AS PItext),
      5   xmlagg(Xmlelement("PO",xmlforest( PO.option_id AS POoid,
      6   PO.question_id AS POqid,
      7   PO.process_id AS popid
      8   ))
      9  ORDER BY PO.option_id))
     10     ORDER BY PI.question_id ) )
     11     FROM liveProcess_ec P
     12  INNER JOIN vw_liveProcessItem_Sim_v6 PI
     13       ON P.process_id = PI.process_id
     14  LEFT OUTER JOIN vw_liveProcessOption_Sim_v6 PO
     15       ON PI.question_id = PO.question_id
     16  AND PI.process_id      = PO.process_id
     17    WHERE p.process_id   =450
     18  GROUP BY p.process_id,PI.question_id,PI.process_id,PI.innertext
     19    ORDER BY p.process_id;
    SELECT xmlelement("P",xmlforest(P.process_id AS Ppid),
                                    *
    ERROR at line 1:
    ORA-00937: not a single-group group function
    Thanks in advance

    I think you are looking for something like this:

    SQL> SELECT
      2      XMLAgg(
      3        XMLElement("P"
      4          ,XMLElement("Ppid",p.process_id)
      5          ,XMLElement("qcount",100)
      6       ,(SELECT
      7           XMLAgg(
      8             XMLELement("PI"
      9               , XMLElement("PIqid", pi.question_id)
     10               , XMLElement("PIpid", pi.process_id)
     11               , XMLElement("PItext", pi.innertext)
     12               , XMLAgg(
     13               XMLElement("PO"
     14                    ,XMLForest(
     15                     po.option_id "POoid"
     16                   , po.question_id "POqid"
     17                   , po.process_id "POpid"
     18                 ) AS "PO"
     19                     )
     20            ) --xmlagg
     21             ) --PI
     22           ) --xmlagg
     23           FROM
     24           VW_LIVEPROCESSITEM_SIM_v6 pi
     25            , VW_LIVEPROCESSOPTION_SIM_v6 po
     26           WHERE p.process_id=pi.process_id
     27             AND pi.question_id=po.question_id(+)
     28        GROUP BY pi.question_id,pi.process_id,pi.innertext
     29        )--select ended
     30        )
     31      ) xm
     32  FROM
     33    liveprocess_ec p
     34  GROUP BY p.process_id
     35  ORDER BY p.process_id;
    

    output

    450 100 1 450 CBB1015 - Router Firewall Settinngs Process 2 450 Is the customers PC Firewall turned off? 1 2 450 2 2 450 3 450 Advise the customer to turn off the PC Firewall in order to continue. Has this been done? 1 3 450 2 3 450 ... ... ... 16 450 Issue Resolved

  • SQL query help needed in the Clause type

    Hello

    I'm currently learning Clause type. How can I write in the clause type to get the result below:

    I have a table like this

    DEPTNO ENAME

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

    20 SMITH

    CELINE 10

    BOND 20

    ALLEN 30

    WARD 30

    20 JONES

    30 MARTIN

    30 BLAKE

    10 CLARK

    SCOTT 20

    10 KING

    30 TURNER

    20 ADAMS

    30 JAMES

    20 FORD

    10 MILLER

    I want the output should look like this

    DEPTNO ENAME

    ------     -----

    CELINE 10, CLARK, KING, MILLER

    20 JONES, ADAMS, BOND, FORD, SCOTT, SMITH

    30 MARTIN, JAMES BLAKE, WARD, TURNER, ALLEN

    Hear is the script I am currently working on.

    with emp_group as

    (

    Select 20 deptno, ename 'SMITH' Union double all the

    choose 10, 'CÉLINE' from dual union all

    Select 20, "BOND" of all the double union

    Select 30, 'ALLEN' from dual union all

    Select 30, 'WARD' of all the double union

    Select 20, "JONES' from dual union all

    Select 30, 'MARTIN' from dual union all

    Select 30, "BLAKE" from dual union all

    choose 10, 'CLARK' from dual union all

    Select 20, 'SCOTT' from dual union all

    choose 10, 'KING' of the dual union all

    Select 30, 'TURNER' from dual union all

    Select 20, 'ADAMS' from dual union all

    Select 30, 'JAMES' of the dual union all

    Select 20, 'FORD' Union double all the

    choose 10, 'MILLER' from dual

    )

    Select

    DEPTNO

    A1

    of emp_group

    model

    ignore the nav

    partition (deptno)

    dimension (ROW_NUMBER() over (PARTITION BY deptno ORDER BY ename DESC) rn)

    measures (ename, LPAD(' ', 100) A1)

    rules)

    A1 [rn > 0] = ename [cv ()],

    A1 [0] = ename [cv ()]

    )

    order by deptno

    ;

    your help is very appreciated.

    Thank you in advance.

    Try this.

    Select

    *

    Of

    (

    with emp_group as

    (

    Select 20 deptno, ename 'SMITH' Union double all the

    choose 10, 'CÉLINE' from dual union all

    Select 20, "BOND" of all the double union

    Select 30, 'ALLEN' from dual union all

    Select 30, 'WARD' of all the double union

    Select 20, "JONES' from dual union all

    Select 30, 'MARTIN' from dual union all

    Select 30, "BLAKE" from dual union all

    choose 10, 'CLARK' from dual union all

    Select 20, 'SCOTT' from dual union all

    choose 10, 'KING' of the dual union all

    Select 30, 'TURNER' from dual union all

    Select 20, 'ADAMS' from dual union all

    Select 30, 'JAMES' of the dual union all

    Select 20, 'FORD' Union double all the

    choose 10, 'MILLER' from dual

    )

    Select

    DEPTNO

    names

    rn

    of emp_group

    model

    ignore the nav

    partition (deptno)

    dimension of)

    ROW_NUMBER() over (PARTITION BY deptno ORDER BY ename) rn

    )

    measures (ename, cast (null as names of varchar2 (40)))

    rules

    (

    names [any] order by desc = ename [cv ()] rn | «, » || names [cv () + 1]

    )

    order by deptno

    )

    where rn = 1

    Best regards, Andrei

  • [Oracle 8i] Need help of a hierarchical query pruning branches

    My problem is that my hierarchical query seems only to cut values that do not meet my criteria, but still includes their children. When my query hits a record that does not meet my criteria, I want it to stop there. I tried including the criteria in just the ' ' a where clause of the query and also put the criteria in clause "connect by" as well, but nothing has been set. Will you please keep in mind that I'm using Oracle 8i, so I can't use some of the "nice" statements for hierarchical queries that they introduced in 9. I'm stuck with "Start With... Connect in "."

    I have examples of tables/data I can post if someone needs to see to help me, but to start with, here my current query:
    SELECT     *
    FROM     (
              SELECT 
                   LEVEL
              ,     c_bill.comp_part_nbr                     AS     c_part_nbr
              ,     (select c_part.part_desc 
                   FROM part c_part 
                   WHERE c_part.part_nbr=c_bill.comp_part_nbr)     AS     c_part_desc
              ,     (SELECT c_part.part_type 
                   FROM part c_part 
                   WHERE c_part.part_nbr=c_bill.comp_part_nbr)      AS     c_part_type
              ,     c_bill.qty_per                          AS     c_qty_per_p
              ,     c_bill.qty_per_type                     AS     c_qty_per_type
              ,     (SELECT c_part.qty_on_hand                
                   FROM part c_part 
                   WHERE c_part.part_nbr=c_bill.comp_part_nbr)      AS     c_qty_on_hand
              ,     c_bill.oper_nbr                     AS     rqd_at_op
              ,     c_bill.comp_off_adj                     AS     rqd_offset
              ,     c_bill.bom_doc_nbr                     AS     p_part_nbr
              ,     (SELECT p_part.qty_on_hand 
                   FROM part p_part 
                   WHERE p_part.part_nbr=c_bill.bom_doc_nbr)      AS     p_qty_on_hand
              FROM 
                   BILL c_bill 
              WHERE 
                   (
                             (c_bill.status           =      'RL')           
                        AND     (c_bill.view_code     IN      ('M','G'))     
                        AND     (c_bill.end_eff_dt     >      SYSDATE)       
                        AND     (c_bill.begn_eff_dt     <=      SYSDATE)
                   ) 
              START WITH c_bill.bom_doc_nbr=RPAD(?,25)
              CONNECT BY PRIOR c_bill.comp_part_nbr=c_bill.bom_doc_nbr
              AND     c_bill.view_code     IN     ('M','G')     
              AND     c_bill.status          =     'RL'
              AND      c_bill.end_eff_dt     >     SYSDATE
              AND     c_bill.begn_eff_dt     <=     SYSDATE     
         ) a
    WHERE     c_part_type = 'M'

    Hello

    The criterion in the outer query

    c_part_type = 'M'
    

    applies only to the results; You can still get the descendants of these lines in the output. This seems to be the only thing that is not repeated in the CONNECT BY clause.

    If you don't want to repeat the criteria in the WHERE clause and the CONECT BY clause, you can apply them once in a view online.
    The following example works in Oracle 8.1:

    SELECT     LPAD ( ' '
              , 3 * (LEVEL - 1)
              ) || ename          AS iname
    ,     empno
    ,     mgr
    FROM     (     -- Begin in-line view of employees named C-Z
         SELECT     ename
         ,     empno
         ,     mgr
         FROM     scott.emp
         WHERE     ename     >= 'C'
         )     -- End in-line view of employees named C-Z
    START WITH     mgr    IS NULL
    CONNECT BY     mgr    = PRIOR     empno
    ;
    

    Not only is excluded BLAKE, but the descendants of JAMES BLAKE, MARTIN, etc. are excluded, too, without repeating the condition.

    Please, post CREATE TABLE and INSERT statements for some examples of data - just enough to show what is the problem - and the results desired from these data.

  • 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.

  • Help in hierarchical query

    Hello

    I have data like this.

    EMPID ENAME MANAGERID
    1A
    2 1
    1 OF 3
    4 2
    5 2
    6 3
    4 OF 7

    And I want to show the data in this way. SUPERMNAGERID is always 1, regardless of the fact that that which is his immediate superior.

    EMPID ENAME MANAGERID SUPERMNAGERID
    1A
    2-1-1
    3-1-1
    4-2-1
    5-2-1
    6-3-1
    7-4-1

    Your help on this would be really useful.

    Edited by: user10827825 may 6, 2012 12:36

    Most inefficient way. If you want to use a hierarchical query, use START WITH and to get the "supermanager" use CONNECT_BY_ROOT:

    WITH t AS
         (SELECT 1 EMPID, 'A' ENAME, NULL MANAGER_ID
            FROM DUAL
          UNION ALL
          SELECT 2 EMPID, 'B' ENAME, 1 MANAGER_ID
            FROM DUAL
          UNION ALL
          SELECT 3 EMPID, 'C' ENAME, 1 MANAGER_ID
            FROM DUAL
          UNION ALL
          SELECT 4 EMPID, 'D' ENAME, 2 MANAGER_ID
            FROM DUAL
            UNION ALL
          SELECT 5 EMPID, 'E' ENAME, 2 MANAGER_ID
            FROM DUAL
          UNION ALL
          SELECT 6 EMPID, 'F' ENAME, 3 MANAGER_ID
            FROM DUAL
          UNION ALL
          SELECT 7 EMPID, 'G' ENAME, 4 MANAGER_ID
            FROM DUAL
            )
    SELECT EMPID, ENAME, MANAGER_ID, CONNECT_BY_ROOT EMPID
      FROM t
      START WITH MANAGER_ID IS NULL
      CONNECT BY MANAGER_ID = PRIOR EMPID
      ORDER BY EMPID;
    

    Compare the number of lines hierarchy is generated by bot queries:

    SQL> WITH t AS
      2       (SELECT 1 EMPID, 'A' ENAME, NULL MANAGER_ID
      3          FROM DUAL
      4        UNION ALL
      5        SELECT 2 EMPID, 'B' ENAME, 1 MANAGER_ID
      6          FROM DUAL
      7        UNION ALL
      8        SELECT 3 EMPID, 'C' ENAME, 1 MANAGER_ID
      9          FROM DUAL
     10        UNION ALL
     11        SELECT 4 EMPID, 'D' ENAME, 2 MANAGER_ID
     12          FROM DUAL
     13          UNION ALL
     14        SELECT 5 EMPID, 'E' ENAME, 2 MANAGER_ID
     15          FROM DUAL
     16        UNION ALL
     17        SELECT 6 EMPID, 'F' ENAME, 3 MANAGER_ID
     18          FROM DUAL
     19        UNION ALL
     20        SELECT 7 EMPID, 'G' ENAME, 4 MANAGER_ID
     21          FROM DUAL
     22          )
     23  SELECT count(*)
     24    FROM t
     25    CONNECT BY PRIOR  MANAGER_ID = EMPID
     26    ORDER BY EMPID;
    
      COUNT(*)
    ----------
            18
    
    SQL> WITH t AS
      2       (SELECT 1 EMPID, 'A' ENAME, NULL MANAGER_ID
      3          FROM DUAL
      4        UNION ALL
      5        SELECT 2 EMPID, 'B' ENAME, 1 MANAGER_ID
      6          FROM DUAL
      7        UNION ALL
      8        SELECT 3 EMPID, 'C' ENAME, 1 MANAGER_ID
      9          FROM DUAL
     10        UNION ALL
     11        SELECT 4 EMPID, 'D' ENAME, 2 MANAGER_ID
     12          FROM DUAL
     13          UNION ALL
     14        SELECT 5 EMPID, 'E' ENAME, 2 MANAGER_ID
     15          FROM DUAL
     16        UNION ALL
     17        SELECT 6 EMPID, 'F' ENAME, 3 MANAGER_ID
     18          FROM DUAL
     19        UNION ALL
     20        SELECT 7 EMPID, 'G' ENAME, 4 MANAGER_ID
     21          FROM DUAL
     22          )
     23  SELECT count(*)
     24    FROM t
     25    START WITH MANAGER_ID IS NULL
     26    CONNECT BY MANAGER_ID = PRIOR EMPID
     27    ORDER BY EMPID;
    
      COUNT(*)
    ----------
             7
    
    SQL> 
    

    Now imagine the table real a couple thousand lines.

    SY.

  • 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.

  • 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.

  • 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

  • Hierarchical query result

    Dear all

    I use Oracle Database 10g and I need a hierarchical query giving following release

    KING

    | __JONES

    |   | __SCOTT

    |   | __ADAMS

    |   | __FORD

    |      | __SMITH

    | __BLAKE

    |   | __ALLEN

    |   | __WARD

    |   | __MARTIN

    |   | __TURNER

    |   | __JAMES

    | __CLARK

    | __MILLER

    Best regards

    Try this

    SELECT CASE WHEN level > 1 THEN LPAD (': _ ', LEVEL,' |) ')

    END | Ename AS ename_tree

    WCP

    START WITH mgr IS NULL

    CONNECT BY PRIOR empno = mgr;

  • 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.

  • Conversion of query MS Access to Oracle

    Hello

    With the help of Oracle 11 g.

    I need to convert query MS Access to Oracle:

    UPDATE AR2000_CREDITS_DEBITS INNER JOIN (APPROVED_CREDITS_DEBITS INNER JOIN CREDIT_DEBIT_TYPE ON APPROVED_CREDITS_DEBITS. CREDIT_DEBIT_TYPE = CREDIT_DEBIT_TYPE. CREDIT_DEBIT_TYPE)

    ON AR2000_CREDITS_DEBITS. CREDIT_DEBIT_ID = APPROVED_CREDITS_DEBITS. CREDIT_DEBIT_ID AND

    AR2000_CREDITS_DEBITS. OBLIGATION_SUB_ACCOUNT = APPROVED_CREDITS_DEBITS. SUB_ACCOUNT)

    SET

    AR2000_CREDITS_DEBITS. CLASSIFICATION_CODE = 'X ',.

    AR2000_CREDITS_DEBITS.comments = 'PB - all fields. "

    WHERE

    AR2000_CREDITS_DEBITS. CLASSIFICATION_CODE is null

    AND AR2000_CREDITS_DEBITS. BASE_AMOUNT = BEFORE_TAX_AMOUNT

    AND LAST_SIGNED_DATE = OBLIGATION_DATE

    AND AR2000_CREDITS_DEBITS.GL_DISTRIBUTION_CODE not Like '% SMB.

    Thank you

    M.R.

    You are not viewing the details of your tables so we do not know where the

    BEFORE_TAX_AMOUNT, LAST_SIGNED_DATE, or OBLIGATION_DATE owned or what relationships are exactly.

    You'll probably look at something on these lines:

    Update arcd ar2000_credit_debits

    Set classification_code = 'X ',.

    Comments = "PB - all fields."

    where classification_code is null

    and gl_distribution_code not like '% SMB.

    and (base_amount, last_signed_date)

    in (select before_tax_amount, obligation_date

    of acd approved_credit_debits

    Join credit_debit_type cdt on (acd.credit_debit_type = cdt.credit_debit_type)

    where arcd.credit_debit_id = acd.credit_debit_id

    and arcd.obligation_sub_account = acd.sub_account

    )

    Although I doubt that it works because it is (there certainly not tested), and we need all the information on the tables and relationships first.

Maybe you are looking for