Hierarchy  Query  to  get  parent  nodes?

Hi Everyone,
I want to write a hierarchy query which should give me the path starting from given node to its parents(Grand parents). below is the sample data and the output what i am expecting. and also the output what i am getting right now from my query.
CREATE TABLE RELATION (PARENT VARCHAR2(5),CHILD VARCHAR2(5) PRIMARY KEY);
--Data for the tree which starts from the root 'A'
Insert into RELATION (PARENT, CHILD) Values (NULL,'A');
Insert into RELATION (PARENT, CHILD) Values ('A', 'B');
Insert into RELATION (PARENT, CHILD) Values ('A', 'C');
Insert into RELATION (PARENT, CHILD) Values ('B', 'D');
Insert into RELATION (PARENT, CHILD) Values ('B', 'E');
Insert into RELATION (PARENT, CHILD) Values ('D', 'F');
Insert into RELATION (PARENT, CHILD) Values ('C', 'G');
--Data for the tree which starts from the root 'H'
Insert into RELATION (PARENT, CHILD) Values (NULL,'H');
Insert into RELATION (PARENT, CHILD) Values ('H', 'I');
Insert into RELATION (PARENT, CHILD) Values ('H', 'J');
Expected Output by passing values as 'F' which gives the path from bottom to up.
A<-B<-D<-F
My Query:
SELECT substr(sys_connect_by_path(child,'<-'),3)
FROM relation
WHERE connect_by_isleaf = 1
START WITH child = 'F'
CONNECT BY PRIOR parent = child
ORDER BY child;
Output of my query:
F<-D<-B<-A
I am getting the output in reverse order. i can use the reverse string function to reverse the string but the problem is the node can also contain the values like 'AC' 'BA'.. in future.
Can anyone please help me in getting the correct output.
Thank you in advance.

I like ListAgg :D
with RELATION(PARENT,CHILD) as(
select NULL,'A' from dual union all
select 'A', 'B' from dual union all
select 'A', 'C' from dual union all
select 'B', 'D' from dual union all
select 'B', 'E' from dual union all
select 'D', 'F' from dual union all
select 'C', 'G' from dual union all
select NULL,'H' from dual union all
select 'H', 'I' from dual union all
select 'H', 'J' from dual)
SELECT ListAgg(child,'<-')
       within group(order by Level desc) as revPath
FROM relation
START WITH child = 'F'
CONNECT BY PRIOR parent = child;
revPath
A<-B<-D<-F

Similar Messages

  • Re: Hierarchy  Query  to  get  parent  nodes?

    Guys i need a help here please....
    i want to query using any single value for example in where clause for  "E" , i want to retrieve whole bunch from "A" to "G" which are all interlinked. is there any way for  this?..
    im lookking like
    A
    B
    C
    D
    E
    F
    G
    when i query for "E"
    Thanks in advance..

    sorry,,, i couldn't explain properly...
    here is the example...
    CREATE TABLE RELATION (PARENT VARCHAR2(5),CHILD VARCHAR2(5) PRIMARY KEY);
    ---this is group 1
    Insert into RELATION (PARENT, CHILD) Values ('A', 'B');
    Insert into RELATION (PARENT, CHILD) Values ('A', 'C');
    Insert into RELATION (PARENT, CHILD) Values ('B', 'D');
    Insert into RELATION (PARENT, CHILD) Values ('B', 'E');
    Insert into RELATION (PARENT, CHILD) Values ('D', 'F');
    Insert into RELATION (PARENT, CHILD) Values ('C', 'G');
    --This is group 2
    Insert into RELATION (PARENT, CHILD) Values ('H', 'I');
    Insert into RELATION (PARENT, CHILD) Values ('H', 'J');
    Insert into RELATION (PARENT, CHILD) Values ('J', 'K');
    Insert into RELATION (PARENT, CHILD) Values ('K', 'M');
    I WILL TAKE  group 1 ..
    i want from relation table single  column all related falling in one group . like..
    when i look for E i should get all the below..
    A
    B
    C
    D
    E
    F
    G
    even though E is not directly linked to A it is linked through B. basically all in one group but the link is not direct. i dont have any group key in my table..
    the query column (where clause) can be either parent or child..
    hope u will gettit..   thanks for ur patience...

  • How to get Parent nodes and corresponding child nodes in BI Hierarchy

    Hi all,
    I have a standard function module 'RSNDI_SHIE_STRUCTURE_GET3'  to get child node if i pass parent node. But I need child nodes along with the provided parent node as I use this in a loop in BI.
    Thanks

    Could any one help me in this regards .
    Thanks in advance
    Regards,
    sri

  • Getting Parent Node using SQL

    Hello,
    I am trying to query an XML which is stored in XMLTYPE datatype column in oracle table. I am using the below SQL:
    select
    EXTRACTVALUE (column_name, '/Person/Customer/Customer_First_Name'),
    EXTRACTVALUE (column_name, '/Person/Customer/Customer_Last_Name'),
    from TABLE_1 A, TABLE ( XMLSEQUENCE (EXTRACT (A.column_name, '/sdi_header//Person')) ) P
    Sample XML:
    <sdi_header>
    <Person>
    <Customer_First_Name>ABC</Customer_First_Name>
    <Customer_Last_Name>DEF</Customer_First_Name>
    </Person>
    <warehouse>
    <Person>
    <Customer_First_Name>XYZ</Customer_First_Name>
    <Customer_Last_Name>MNO</Customer_First_Name>
    </Person>
    </warehouse>
    </sdi_header>
    In the above query the Alias P has a //Person which means I am trying to extract all the repeating person Nodes information in XML. Retrieving all persons is working but I want to extract the parent node information of each Person like for Example in the above sample XML, it has two persons in it
    1) 1 Person is coming under sdi_header node , in such case I need to get <sdi_header/> in SELECT statement
    2) 1 Person is coming under warehouse node , in such case I need to get <warehouse/>in SELECT statement
    How can the parent node information be extracted in SELECT statement.
    Any suggestion/Help is much appreciated.
    Thanks,
    SKM
    Edited by: user9116854 on Mar 1, 2010 10:24 AM
    Edited by: user9116854 on Mar 1, 2010 10:30 AM
    Edited by: user9116854 on Mar 1, 2010 10:35 AM
    Edited by: user9116854 on Mar 1, 2010 10:45 AM
    Edited by: user9116854 on Mar 2, 2010 8:05 AM

    Sounds like you are looking for something like this
    Connected to Oracle Database 11g Enterprise Edition Release 11.1.0.6.0
    SQL> WITH table_1 AS
      2  (SELECT XMLTYPE('<sdi_header>
      3  <Person>
      4  <Customer_First_Name>ABC</Customer_First_Name>
      5  <Customer_Last_Name>DEF</Customer_Last_Name>
      6  </Person>
      7  <warehouse>
      8  <Person>
      9  <Customer_First_Name>XYZ</Customer_First_Name>
    10  <Customer_Last_Name>MNO</Customer_Last_Name>
    11  </Person>
    12  </warehouse>
    13  </sdi_header>') column_name
    14     FROM dual)  -- WITH simulates your table I do not have
    15  select t1.*
    16    from TABLE_1,
    17         XMLTABLE('for $i in /sdi_header//Person
    18                     return <root>
    19                           {$i/Customer_First_Name,
    20                           $i/Customer_Last_Name}
    21                           <parent>{$i/../name()}</parent>
    22                      </root>'
    23                  PASSING table_1.column_name
    24                  COLUMNS
    25                  fname   VARCHAR2(20) PATH 'Customer_First_Name',
    26                  lname   VARCHAR2(20) PATH 'Customer_Last_Name',
    27                  pnode   VARCHAR2(20) PATH 'parent') t1;
    FNAME                LNAME                PNODE
    ABC                  DEF                  sdi_header
    XYZ                  MNO                  warehouseYou can find out how formatted that message by looking in the FAQ in the upper right. It uses the { code } tag (without spaces).

  • Run query and get unexpected node values

    Hi,
    I am not seeing what should be expected when I run my query, and I am getting the display of the hierarchy nodes that I am not supposed to get access to. I checked my profile and I have the 0COSTCENTER as '*' and TCTAUTHH as ':' .
    Where should I start finding why I am getting the node values displayed that I am not supposed to?
    Thanks
    Will

    Welcome to SDN.
    I think you should better post it to the BI forum.
    Did you try to use the user exit EXIT_SAPLRRS0_001 (extension RSR00001) to initialize the values? And for bypassing the selection screen, can't you define it in the query definition (if I remember well)?

  • Get Parent node from Base member

    Hi ,
    My user select base member from CV .
    I want parent node for that base member .
    I am using =EVPRO( App name , memmber id , "PARENTH1" ) .
    I am not getting parent member from this funcation .
    Pls let me know any other way for this ?
    regards,
    PSR

    Hi,
    EVPRO for getting the value for PARENTH1 of any member should work. Please check whether you have included all the parameters of this function inside double quotes.
    EVPRO("APPNAME","MEMBER_ID","PROPERTY")
    Hope this helps,
    Regards,
    G.Vijaya Kumar

  • Getting Parent Node On tree Issue..

    Hi,
    I Have Constructed a Tree and i am Able to get the Details of the clicked Node on the Tree into the Backing Bean.
    But I'm unable to get the Parent Node of the Clicked Child Node.
    Ex:On Click of Employee(Child Node) in the Tree, I should get the Department (Parent Node) in Backing Bean.
    I'm able to get only the details in the same Level of Tree Node(ie All Employees in the Selected Employee Level).
    IDE: JDeveloper 10.1.3.2.
    Please Suggest me.
    Thanking You,
    Bandaru.

    Sounds like you are looking for something like this
    Connected to Oracle Database 11g Enterprise Edition Release 11.1.0.6.0
    SQL> WITH table_1 AS
      2  (SELECT XMLTYPE('<sdi_header>
      3  <Person>
      4  <Customer_First_Name>ABC</Customer_First_Name>
      5  <Customer_Last_Name>DEF</Customer_Last_Name>
      6  </Person>
      7  <warehouse>
      8  <Person>
      9  <Customer_First_Name>XYZ</Customer_First_Name>
    10  <Customer_Last_Name>MNO</Customer_Last_Name>
    11  </Person>
    12  </warehouse>
    13  </sdi_header>') column_name
    14     FROM dual)  -- WITH simulates your table I do not have
    15  select t1.*
    16    from TABLE_1,
    17         XMLTABLE('for $i in /sdi_header//Person
    18                     return <root>
    19                           {$i/Customer_First_Name,
    20                           $i/Customer_Last_Name}
    21                           <parent>{$i/../name()}</parent>
    22                      </root>'
    23                  PASSING table_1.column_name
    24                  COLUMNS
    25                  fname   VARCHAR2(20) PATH 'Customer_First_Name',
    26                  lname   VARCHAR2(20) PATH 'Customer_Last_Name',
    27                  pnode   VARCHAR2(20) PATH 'parent') t1;
    FNAME                LNAME                PNODE
    ABC                  DEF                  sdi_header
    XYZ                  MNO                  warehouseYou can find out how formatted that message by looking in the FAQ in the upper right. It uses the { code } tag (without spaces).

  • Xpath Query to find parent nodes

    My XMl is like this
    <UserAgent1>
    <Name>QTS </Name>
    <NetworkFamily>GSM</NetworkFamily>
    <NetworkSupportedMap>0x06</NetworkSupportedMap>
    </UserAgent1>
    <UserAgent2>
    <Name>QuickTime</Name>
    <NetworkFamily>GSM</NetworkFamily>
    <NetworkSupportedMap>0x06</NetworkSupportedMap>
    </UserAgent2>
    Now if they give a search String like "GSM" without specifying any tag
    Then i use the xpath query
    //*[contains(text(),'GSM')]
    This will give me the child element <NetworkFamily>GSM</NetworkFamily> . .and with this i will find out the parent element which is <UserAgent1> & <UserAgent2>
    IS there any query which will give me directly the Parent Nodes whose childnodes contain the given text ?

    Well, of course there is. But I couldn't remember how so I googled the keywords "xpath parent". The first link that came back suggested
    //*[contains(text(),'GSM')]/..
    and the second suggested the parent axis
    parent::(//*[contains(text(),'GSM')]) or maybe //parent::*[contains(text(),'GSM')]
    So yes, there's several ways. And they were easy to find with Google too.

  • HOw to get parent node name value through its child node?

    Hi,
    Experts,
    I am able to get child node name values but according to attribute name value i want to  get its parent name value how to achieve that. For that i have used If_Ixml_element->Get_parent. Please pass some idea on it.
    Thanks in advance,
    Shabeer ahmed.

    Hello Shabeer
    I think the coding should be straightforward:
    DATA: lo_element   TYPE REF TO if_ixml_element,
              lo_child        TYPE REF TO if_ixml_node,
              lo_parent      TYPE REF TO if_ixml_node.
    " NOTE: LO_ELEMENT holds your child node
      lo_child ?= lo_element.
      lo_parent = lo_child->get_parent( ).
    Regards
      Uwe

  • Using xpath to get parent nodes

    hi! i�m trying to copy some elements from a xml file, and i have some problems.
    i have this file
    ?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="xslpath.xsl"?>
    <bookstore>
    <book id="0">
    <title id="pt">Harry Magico</title>
    <desc id="1">
    <price id="3">29.99</price>
    <price1 id="3">329.95</price1>
    </desc>
    <name>nome</name>
    </book>
    </bookstore>
    i�m using a xsl to copy the nodes.
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
    <xsl:copy-of select="/bookstore/book/title/ancestor-or-self::price1"/> <!--not correct-->
    </xsl:template>
    </xsl:stylesheet>
    well, if i select price1 node, what i want to get is:
    <?xml version="1.0" encoding="utf-8"?>
    <bookstore>
    <book id="0">
    <desc id="1">
    <price1 id="3">329.95</price1>
    </desc>
    </book>
    </bookstore>
    i just want copy the node without is "brothers" :) and with is parents.
    can anyone help me?

    hi! i�m trying to copy some elements from a xml file,
    and i have some problems.
    i have this file
    ?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl"
    href="xslpath.xsl"?>
    <bookstore>
    <book id="0">
    <title id="pt">Harry Magico</title>
    <desc id="1">
    <price id="3">29.99</price>
    <price1 id="3">329.95</price1>
    </desc>
    <name>nome</name>
    </book>
    </bookstore>
    i�m using a xsl to copy the nodes.
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="/">
    <xsl:copy-of
    select="/bookstore/book/title/ancestor-or-self::price
    "/> <!--not correct-->
    </xsl:template>
    l:stylesheet>
    well, if i select price1 node, what i want to get
    is:
    <?xml version="1.0" encoding="utf-8"?>
    <bookstore>
    <book id="0">
    <desc id="1">
    <price1 id="3">329.95</price1>
    </desc>
    </book>
    </bookstore>
    i just want copy the node without is "brothers" :)
    and with is parents.
    can anyone help me?right off my head, try this:
    /bookstore/book/title/desc/price1/ancestor-or-self::*

  • SRM Get parent node

    Dear experts,
    I need to know a FM or BAPI in order to know the parent objetc/record/document that contains an element.
    SAP provides the BAPI BAPI_RECORD_GETELEMENTS to get the elements of a record, but I need the opposite operation (get the record that contains the element).
    Thanks in advance.
    Best regards,
    A. Cepa

    Hi,
    EVPRO for getting the value for PARENTH1 of any member should work. Please check whether you have included all the parameters of this function inside double quotes.
    EVPRO("APPNAME","MEMBER_ID","PROPERTY")
    Hope this helps,
    Regards,
    G.Vijaya Kumar

  • Hierarchy display child and parent nodes -- Urgent Please

    Sorry wrong forum
    Message was edited by:
            Shree Sunder

    I like ListAgg :D
    with RELATION(PARENT,CHILD) as(
    select NULL,'A' from dual union all
    select 'A', 'B' from dual union all
    select 'A', 'C' from dual union all
    select 'B', 'D' from dual union all
    select 'B', 'E' from dual union all
    select 'D', 'F' from dual union all
    select 'C', 'G' from dual union all
    select NULL,'H' from dual union all
    select 'H', 'I' from dual union all
    select 'H', 'J' from dual)
    SELECT ListAgg(child,'<-')
           within group(order by Level desc) as revPath
    FROM relation
    START WITH child = 'F'
    CONNECT BY PRIOR parent = child;
    revPath
    A<-B<-D<-F

  • Need query to get parent and child relations

    Hi,
    SELECT
    a.ID MASTER_ID, a.UNIQUE_NAME MASTER_UNIQUE_NAME, d.ID CHILD_ID, d.UNIQUE_NAME CHILD_UNIQUE_NAME, 1 SUB_LEVEL
    FROM
    srm_projects a,
    prTask b,
    prSubProject c,
    srm_projects d
    WHERE
    a.id = b.prProjectID
    AND b.prID = c.prTaskID
    AND c.PRREFPROJECTID=d.id
    The above query gives a project and its child.
    Child project can inturn have child projects.
    But the above query gives only the first level.
    To obtain second level I am presently using the following query
    SELECT
    a.ID MASTER_ID, a.UNIQUE_NAME MASTER_UNIQUE_NAME, d.ID CHILD_ID, d.UNIQUE_NAME CHILD_UNIQUE_NAME, 1 SUB_LEVEL
    FROM
    srm_projects a,
    prTask b,
    prSubProject c,
    srm_projects d
    WHERE
    a.id = b.prProjectID
    AND b.prID = c.prTaskID
    AND c.PRREFPROJECTID=d.id
    UNION ALL
    SELECT
    a.ID, a.UNIQUE_NAME, g.ID, g.UNIQUE_NAME, 2 SUB_LEVEL
    FROM
    srm_projects a,
    prTask b,
    prSubProject c,
    srm_projects d,
    prSubProject e,
    prTask f,
    srm_projects g
    WHERE
    a.id = b.prProjectID
    AND b.prID = c.prTaskID
    AND c.PRREFPROJECTID=d.id
    AND d.ID = f.PRPROJECTID
    AND f.prID = e.prTaskID
    AND e.PRREFPROJECTID=g.id
    But the problem is there is no limit on levels. They can go on to 20 to 30.
    So i need a query which gives me all the levels and not as above one where i am specifying till wht level.
    Please guide me.

    Thanks.
    Please find the table structures
    CREATE TABLE SRM_PROJECTS
    ID NUMBER NOT NULL,
    NAME VARCHAR2(240 BYTE),
    UNIQUE_NAME VARCHAR2(60 BYTE),
    DESCRIPTION VARCHAR2(2286 BYTE),
    IS_ACTIVE NUMBER DEFAULT 1 NOT NULL,
    CREATED_DATE DATE NOT NULL,
    CREATED_BY NUMBER NOT NULL,
    LAST_UPDATED_DATE DATE NOT NULL,
    LAST_UPDATED_BY NUMBER NOT NULL,
    CREATE TABLE PRTASK
    PRUID VARCHAR2(32 BYTE),
    PRID NUMBER(10),
    PRPROJECTID NUMBER(10),
    PRISUNPLANNED NUMBER(10) DEFAULT 0 NOT NULL,
    PRSHORTNAME VARCHAR2(48 BYTE),
    PRNAME VARCHAR2(450 BYTE),
    PREXTERNALID VARCHAR2(48 BYTE),
    PRISMILESTONE NUMBER(10) DEFAULT 0 NOT NULL,
    PRCATEGORY VARCHAR2(96 BYTE),
    CREATE TABLE PRSUBPROJECT
    PRUID VARCHAR2(32 BYTE),
    PRID NUMBER(10),
    PRTASKID NUMBER(10),
    PRREFPROJECTID NUMBER(10),
    PRREFTASKID NUMBER(10),
    PRISREADONLY NUMBER(10) DEFAULT 0 NOT NULL,
    PRISIPD NUMBER(10) DEFAULT 0 NOT NULL,
    PRMODBY VARCHAR2(96 BYTE),
    PRMODTIME DATE
    )

  • Problem with Jtree to xml tranform..how to set/get parent of a node?

    Hi,
    I am trying to develop xml import/export module.In import wizard, I am parsing the xml file and the display it in Jtree view using xml tree model which implements TreeModel and xml tree node.I am using jaxp api..
    It is workin fine.
    I got stuck with removal of selected node and save it as a new xml.
    I am not able to get parent node of selected node in remove process,itz throwing null.I think i missed to define parent when i load treemodel.Plz help me out..give some ideas to do it..
    thanks
    -bala
    Edited by: r_bala on May 9, 2008 4:44 AM

    there's no way anyone can help you without seeing your code.

  • Parent nodes have themselves as childs, in contrast to BW hierarchy

    Hello Gurus,
    I have a problem concerning hierarchies and grouping in CR2008.
    Inserting the hierarchy works fine, but here's the problem:
    Every parent node has itself as child node, i.e. the hierarchy shown is:
    1
    1.1
    1.2
    1.2.2
    1.2.3
    instead of the correct:
    1
    1.2
    1.2.3
    That even occurs when I just include the Node ID, without any grouping and hierarchy settings.
    Such behaviour is undesirable for my purpose, so how can I make Crystal Reports behave like I expect/want? I am completely confused where these additional nodes are originating, as they are definitely not defined in the BW hierarchy. Therefore I suspect the problem somewhere in Crystal, although I have no actual evidence supporting this...
    Has anyone an idea how to solve that problem? I have crawled through SDN and unfortunately wasn't able to find a solution...
    Thanks!

    Hi Ingo,
    first I'd like to apologize for the late reply, I was very busy with another project during the last weeks.
    It seems to be the bookable nodes setting in the query, at least only such parent nodes are affected that have data posted to themselves. Obviously, the actual parent node as shown in Crystal is the one with data aggregated by Crystal and the child node is the one with the data that was posted to the node.
    I'll try to find some free time during the remainder of the week for further investigation and provide additional feedback.
    Thanks for your help so far!

Maybe you are looking for

  • Extended features error

    I have created a form in Adobe acrobat 10 and produced a fillable form. This is used by students who use it using Reader (various versions) over the period of their course and revisit it and save it on multiple occations. For some students it is fine

  • Getting 0Analysis_Pattern_Info to work

    Hi there What do I need to do in order to get the standard template 0Analysis_Pattern_Info to work? This template is called when the Information button is clicked, but at the moment all I am getting is a short dump error message. Cheers, Andrew

  • Chapter TOCs

    To preface, I'm at best a journeyman with FrameMaker and haven't used it in something like three years. Remember the basic stuff, the finer points and cool things that make FrameMaker wonderful...forget it. Just so you know what you're dealing with (

  • Career with BI/BW

    Hi, Sorry. This is a non general question. I am presently in UK. I have experience of 8+ years in other technologies & ERP. I wish to pursure career with BI/BW. I am trying for an online course with dummy assignments to learn SAP BI/BW. My question i

  • Exportfunktion von Bridge speichert alles in srgb

    Es um die Exportfunktion von Bridge. Hier ziehe ich die Bilder hin, die später zur Entwicklung oder zum Drucker sollen. Egal ob die Bilder in Camera Raw mit Farbraum Adobe RGB, als TIF in Adobe RGB oder sofern Schwarzweiß in Gamma 2.2 vorliegen, habe