hold the hierarchical querry with node current level 0 (zero)

I had created and inserted values into the table
create table emp_id
( 
 head_id number(4),
 tail_id number(4) 
 );
 
insert into emp_id values (1011,1008)
insert into emp_id values (1008,1003)
insert into emp_id values (1012,1003)
insert into emp_id values  (1020,1003)
insert into emp_id values (1025,1003)
insert into emp_id values (1015,1012)
now I want to get the hierarchy starting with 1012 as shown below
select     level,dt.head_id
from       emp_id dt
          start with dt.tail_id = 1012        
connect by prior  dt.head_id= dt.tail_id 


output is like this :


LEVEL     HEAD_ID

1     1008
2     1011
1     1012
2     1015
1     1020
1     1025
but I want to inlcude 1003 in the output level 0

How can I do?

Thanks in advance

Hello

482 wrote:
I had created and inserted values into the table

create table emp_id
(
head_id number(4),
tail_id number(4)
);

insert into emp_id values (1011,1008)
insert into emp_id values (1008,1003)
insert into emp_id values (1012,1003)
insert into emp_id values  (1020,1003)
insert into emp_id values (1025,1003)
insert into emp_id values (1015,1012)

Thanks for posting the sample data in a useful form. That really helps.

now I want to get the hierarchy starting with 1012 as shown below

select     level,dt.head_id
from       emp_id dt
start with dt.tail_id = 1012
connect by prior  dt.head_id= dt.tail_id 

output is like this :

LEVEL     HEAD_ID

1     1008
2     1011
1     1012
2     1015
1     1020
1     1025

Make sure that it is the hierarchy start at 10 h 12 and no 1003?
>

but I want to inlcude 1003 in the output level 0

How can I do?

Thanks in advance

What you have is a single Arc table, where you have a line by the relationship between identifiers.
Classic CONNECT BY work on base of knot tables, with a line by id.
In a tree (for example your data), each id except the root is the child (head_id) or a relationship, so a node database tables will be all lines with a rainbow table only, as well as a line for each root.

You can simulate a node database table by adding a line to the root by using the UNION, as shown below.
You can add the real lines like that permanently to your table. They are easy to ignore when you don't want them, and they do queries like this simpler and more effective.

WITH     got_root     AS
(
     SELECT     head_id
     ,     tail_id
     FROM     emp_id
          --
     UNION ALL
             --
     SELECT  1003     AS head_id
     ,     NULL     AS tail_id
     FROM     dual
)
SELECT     LEVEL - 1     AS lvl
,     head_id
FROM     got_root
START WITH     tail_id       IS NULL
CONNECT BY     tail_id       = PRIOR head_id
;

Output of your sample data:

.      LVL    HEAD_ID
---------- ----------
         0       1003
         1       1008
         2       1011
         1       1012
         2       1015
         1       1020
         1       1025

It works for the test case, you have published and can easily be adapted to handle a number any of given points of departure, not necessarily of roots.

Tags: Database

Similar Questions

  • dimension of the hierarchical attribute with Essbase Studio

    Hello world

    I need to load the product dimension that is organized in a parent-child relational table like this:
    (parent_code, member_code, member_alias, mark, consolidation, formula)

    Each product has a mark and I need to load brand as a dimension of the attribute. The thing is the brand isn't flat, but it has its own hierarchy. For the brand, I have another relational table, where the data are organized parent-child like this:
    (brand_parent, brand_child, brand_alias).

    I do this load with Essbase Studio but I know is not how to create the Product hierarchy in the cube schema in order to load the parent-child hierarchical attributes. Documentation and tutorials only show examples of dishes attributes. I thought of these two ways, but do not know if will work:

    hierarchy of case 1) product like this:

    Parent_code
    Member_code
    Brand_parent
    Brand_child
    Member_code

    hierarchy of case 2) product like this:

    Parent_code
    Member_code
    Brand_parent
    Brand
    Member_code

    Someone had to do this? How did you do?
    Any help will be much appreciated.

    Thank you
    Daniela

    Hello Daniela,
    Please see the following post. One of my colleagues ran into the same issue, and his post was answered in the post below.
    ESBase Studio question - how to set up the dim attribute?

    Thank you
    Daniel
    http://dvepm.com

  • Refresh the hierarchical planning with maxi?

    Is it possible to script and automate a refresh of planning database? Perhaps with MAXL?

    Not with Maxl, but yes, there is a command line utility to achieve do - updating of databases with the help of a utility Application

    See you soon

    John

    http://John-Goodwin.blogspot.com/

  • Building the tree balanced with SQL hierarchical queries

    Hi all

    I have the following hierarchical data with different levels of subtree:

    A0
    -A001
    -A00101
    A1
    -A101
    A2
    -A201
    -A20101
    -A201010001

    A0 subtree has 3 levels, A1 subtree has 2 levels and subtree of the A3 is level 4. I want to generate a tree balanced on the data with all levels of the subtree equal to the maximum number of levels available in the whole tree which, in this particular case, is 4.

    I don't know that it is possible with SQL. Script to generate the above mentioned are as below:

    CREATE TABLE codes_tree
    (node_id VARCHAR2 (10))
    parent_node_id VARCHAR2 (10)
    );
    INSERT INTO codes_tree VALUES ('A0', NULL);
    INSERT INTO codes_tree VALUES ('A001', 'A0');
    INSERT INTO codes_tree VALUES ('A00101', 'A001');
    ---
    INSERT INTO codes_tree VALUES ('A1', NULL);
    INSERT INTO codes_tree VALUES ('A101', 'A1');
    ---
    INSERT INTO codes_tree VALUES ('A2', NULL);
    INSERT INTO codes_tree VALUES ('A201', 'A2');
    INSERT INTO codes_tree VALUES ('A20101', 'A201');
    INSERT INTO codes_tree VALUES ('A201010001', 'A20101');

    Any help will be much appreciated.

    Thank you... Best regards

    Published by: naive2Oracle on May 12, 2011 19:40

    Published by: naive2Oracle on May 12, 2011 19:41

    Hello

    Of course, you can do it in SQL.
    One way is to take the normal output of hierarchical and manipulate the result set so that the leaves are repeated as often as necessary to make all branches of the same length. I have Oracle 10.2 available right now, so here's a solution that will work in Oracle 10 (and more):

    WITH     original_hierarchy     AS
    (
         SELECT     node_id
         ,     LEVEL               AS lvl
         ,     CONNECT_BY_ISLEAF     AS isleaf
         ,     ROWNUM               AS rnum
         FROM     codes_tree
         START WITH     parent_node_id     IS NULL
         CONNECT BY     parent_node_id     = PRIOR node_id
    )
    ,     got_max_lvl     AS
    (
         SELECT     o.*
         ,     MAX (lvl) OVER ()     AS max_lvl
         FROM     original_hierarchy     o
    )
    SELECT       LPAD ( ' '
                , 3 * ( ( d.lvl
                     + NVL (c.rnum, 1)
                     - 1
                     )
                   - 1
                   )
                ) || CASE
                   WHEN c.rnum > 1
                   THEN '*' || d.node_id || '*'
                   ELSE        d.node_id
                  END          AS display_id
    FROM            got_max_lvl     d
    LEFT OUTER JOIN       got_max_lvl     c  ON     d.isleaf     = 1
                           AND     c.rnum          <= 1 + d.max_lvl - d.lvl
    ORDER BY  d.rnum
    ,       c.rnum
    ;
    

    With the help of Oracle 11.2, it would be preferable to generate original_hierarchy as above, but to manipulate using a WITH recursive clause.
    Analytical functions often interfere with CONNECT BY, so I used a separate subquery to get max_lvl, do CONNECT BY in a sub-querry and analytic function in a separate subquery. I don't know what is needed on all versions.

    Output of your sample data:

    DISPLAY_ID
    -------------------------------
    A0
       A001
          A00101
             *A00101*
    A1
       A101
          *A101*
             *A101*
    A2
       A201
          A20101
             A201010001
    

    Below is a generic version of the same query, which I used to test this on scott.emp:

    DEFINE     table_name     = scott.emp
    DEFINE     id_col          = empno
    DEFINE     parent_id_col     = mgr
    DEFINE     display_col     = ename
    
    WITH     original_hierarchy     AS
    (
         SELECT     &display_col          AS display_txt
         ,     LEVEL               AS lvl
         ,     CONNECT_BY_ISLEAF     AS isleaf
         ,     ROWNUM               AS rnum
         FROM     &table_name
         START WITH     &parent_id_col     IS NULL
         CONNECT BY     &parent_id_col     = PRIOR &id_col
    )
    ,     got_max_lvl     AS
    (
         SELECT     o.*
         ,     MAX (lvl) OVER ()     AS max_lvl
         FROM     original_hierarchy     o
    )
    SELECT       LPAD ( ' '
                , 3 * ( ( d.lvl
                     + NVL (c.rnum, 1)
                     - 1
                     )
                   - 1
                   )
                ) || CASE
                   WHEN c.rnum > 1
                   THEN '*' || d.display_txt || '*'
                   ELSE        d.display_txt
                  END          AS display_id
    FROM            got_max_lvl     d
    LEFT OUTER JOIN       got_max_lvl     c  ON     d.isleaf     = 1
                           AND     c.rnum          <= 1 + d.max_lvl - d.lvl
    ORDER BY  d.rnum
    ,       c.rnum
    ;
    

    Output:

    DISPLAY_ID
    -----------------------------
    KING
       JONES
          SCOTT
             ADAMS
          FORD
             SMITH
       BLAKE
          ALLEN
             *ALLEN*
          WARD
             *WARD*
          MARTIN
             *MARTIN*
          TURNER
             *TURNER*
          JAMES
             *JAMES*
       CLARK
          MILLER
             *MILLER*
    

    Published by: Frank Kulash, May 13, 2011 06:38
    Adding the generic version

  • We have SQL Server 2005 cluster (3 node cluster), and if we improve this nodes to 2008 R2, the databases associated with the named instance can stay to the version of SQL Server 2005?

    original title: SQL Server 2008

    We have SQL Server 2005 cluster (3 node cluster) and if we improve this nodes to 2008 R2, can the databases associated with the named instance remain to the version of SQL Server 2005, I seem to disagree with this notion, but still need to validation of the pro.

    One way I can think of to achieve this scenario changes the compatibility level to SQL Server 2005 when the node has been upgraded to SQL Server 2008, so we can upgrade databases to SQL 2008 as and when the vendors provide support for this version.

    Hello

    Here is the Vista forums

    Try the links below:

    SQL Server forums

    http://social.technet.Microsoft.com/forums/en-us/category/SQLServer/

    SQL Server TechCenter

    http://TechNet.Microsoft.com/en-us/SQLServer/bb265254.aspx

    Blogs and Forums SQL server 2008

    http://www.Microsoft.com/sqlserver/2008/en/us/forums-blogs.aspx

  • How to find the current level of the tree in oracle forms?

    Hi, I want to find the current level of the tree when the user click on a node wants to find current level how to do this?

    Dear Mohammed,

    You can use the FTREE. GET_TREE_NODE_PROPERTY ('tree_block.tree_name',: SYSTEM.) TRIGGER_NODE, FTREE. NODE_DEPTH); To achieve the current level of the node.

    Kind regards

    Manu.

  • How, in Illustrator CS5, select all objects with the three points (or nodes)?

    I'm fairly new to Illustrator, but it seems like it should be simple.  (It's trivial in CorelDraw X 4: after selecting an object with three knots, I go to Edition > search and replace > find objects > find objects that correspond to the currently selected object and navigate the menus left.)

    In Illustrator CS5, select options > identical do not seem to include the selection of all objects with the same number of nodes under the currently selected object.

    A double click on the magic wand tool also does not provide such an option.

    Thanks for any help.

    PS also useful would be knowing how to add other criteria to this search, for example, how to select all objects with three points * and * the current fill color.

    advictoriam,

    I'm afraid that you have better luck with the number of anchor Point.

  • Color of text in af:tree with the definitions of two nodes

    Hi all

    I use JDev 11.1.1.5.0

    I have an af:tree component that got two definitions of nodes (two different iterators). I want to display the data in other colors with other icons etc. According to the definition of node.
      <tree IterBinding="OrganizacjeGlowneIterator" id="OrganizacjeGlowne">
          <nodeDefinition DefName="proby.model.views.ElorganizacjeView"
                          Name="OrganizacjeGlowne0"
                          TargetIterator="${bindings.OrganizacjeEditIterator}">
            <AttrNames>
              <Item Value="Kod"/>
              <Item Value="Nazwa"/>
              <Item Value="Typorganizacji"/>
            </AttrNames>
            <Accessors>
              <Item Value="ElorganizacjeView"
                    Label="strukturaOrganizacyjnaPageDef_OrganizacjeGlowne_proby.model.views.ElorganizacjeView_ElorganizacjeView"/>
              <Item Value="StanowiskaUsersView"
                    Label="strukturaOrganizacyjnaPageDef_OrganizacjeGlowne_proby.model.views.ElorganizacjeView_StanowiskaUsersView"/>
            </Accessors>
          </nodeDefinition>
          <nodeDefinition DefName="proby.model.views.StanowiskaUsersView"
                          Name="OrganizacjeGlowne1"
                          TargetIterator="${bindings.StanowiskaUsersEditIterator}">
            <AttrNames>
              <Item Value="NazwaStanowiska"/>
              <Item Value="ImieUsera"/>
              <Item Value="NazwiskoUsera"/>
              <Item Value="EmailUsera"/>
            </AttrNames>
            <Accessors>
              <Item Value="StanowiskaUsersView_2"/>
            </Accessors>
          </nodeDefinition>
        </tree> 
    I wanted to use EL in the Style section in the properties of the af Component Inspector: tree, but I don't know how to get the node current definition name.
    How to change color dynamically based on the current definition of node?

    Any help will be appreciated.

    Kind regards
    Wojtek.

    Hello

    This EL

    #{node.hierTypeBinding.structureDefName}
    

    Returns the name of the name of the View node and package object

    If you could do

    #{node.hierTypeBinding.structureDefName == "proby.model.views.ElorganizacjeView"? Red: white}

    for the nodes of color code

    Frank

  • How make the visible context for nodes in the level menu gave treeTable?

    Hello

    I use JDeveloper 11 g 10.1.1.4

    I have a TreeTable component with 4 different levels (Contry-branch-city-object).
    I need to show the custom menu contextual (right-click the Muse) items only for lines with 'object '.
    Now, I do it with the help of the "visible" property
                <af:commandMenuItem text="Open" id="cmi1" immediate="true"
                                    visible="#{backingBeanScope.mbMObjectsRegistry.MObjectNode}"
                                    actionListener="#{backingBeanScope.mbMObjectsRegistry.btOpenMObject}"/>
        public boolean isMObjectNode() {
            Object oldRowKey = treeTableMObjectsRegistry.getRowKey();
            try {
                AppViewRowImpl row =
                    BindingUtils.getSelectedRowTreeTable(treeTableMObjectsRegistry);
                if (row == null || !isMOBjectRow(row)) {
                    return false;
                }
                return isMOBjectRow(row);
            } finally {
                treeTableMObjectsRegistry.setRowKey(oldRowKey);
            }
        }
    
        private boolean isMOBjectRow(AppViewRowImpl row) {
            return (row != null) && (row instanceof MObjectsRegisterMORowImpl);
        }
    It works, but the problem is that it works a little more slow, particularly if I have 5 or more menu items.
    Each menu item called the isMObjectNode() metod 3 times by opening the menu.

    The question is: maybe, there is a method to calculate the level of the selected node? Or there is another variant?

    Anatolii

    The following example allows you to develop your usecase.
    Here, we would show the menu items only for the nodes of level 3.

    You can also download all of the POJO from sample to
    http://adfsampleapplications.googlecode.com/svn/trunk/TreeSampleApplication.zip

    The code is self-explanatory.
    Let know, if you have any specific questions.



    selectionListener = "#{bindings.continents.treeModel.makeCurrent} '"
    rowSelection = "single" id = "tt1" columnStretching = "last" >








    **
    **
    *
    * to = "#{pageFlowScope.CurrentDepth}" *.
    * Type = "popupFetch" / >. *
    **
    *
    * visible = "#{pageFlowScope.CurrentDepth == 2? true: false}" / > * "
    *
    * visible = "#{pageFlowScope.CurrentDepth == 2? true: false}" / > * "
    *
    *
    *
    *
    *
    *

    Thank you
    Nini

  • You can restore the photos of the recently deleted items folder? I have an iCloud back for my iPad that I restored it that would have photos on them but then sync with my current iCloud files. Can I restore the last backup of iCloud?

    You can restore the photos of the recently deleted items folder? I have an iCloud back for my iPad that I restored it that would have photos on them but then sync with my current iCloud files. Can I restore the last backup of iCloud?

    The backup to iCloud for an iPad does not include items that are already in iCloud, your iCloud stream library and my photos are not included in the backup to iCloud.

  • I need to finish a powerpoint presentation for the school, even if I don't have it on my windows xp 2003. Is there a free trial of powerpoint that I can download to work with my current operating system? __

    I currently installed on my pc Windows XP 2003, but I need to use Powerpoint to a duty arising today and I wonder if there is a free trial of Powerpoint that I can download that is compatible with my current operating system?

    See: http://office.microsoft.com/en-au/powerpoint/default.aspx

    Microsoft Office PowerPoint 2007
     
    PowerPoint 2007
    Create dynamic and high-impact presentations faster than ever. Start with the new PowerPoint. Download it now, test it in your browser, or buy it today.

    TaurArian [MVP] 2005-2010 - Update Services

  • I can't activate Windows 8 Enterprise. The message I get is "Windows can not be activated right now..." "with a current product key of" *-cwck7 '.

    I can't activate Windows 8 Enterprise.  The message I get is "Windows can not be activated right now..." "with a current product key of" *-cwck7 '. I received this from my MSDN Download. It installed without any problem and seems to work other than this problem. Thank you

    If you are using distributions of Windows 8 volume license, you can use the following methods. If your license is using KMS licenses, talk with your administrator COMPUTER. The following method illustrates product activation using a MAK (Multiple Activation key) key

    Make sure you get a key activate several of your MSDN, Technet or volume license subscription

    Click with the right button on the left corner of the screen of Windows 8

    Click prompt (Admin)

    Type: slmgr.vbs - ipk

    Press enter

    Then type:

    slmgr.vbs - ato

  • I think I bought the wrong to adobe. I was hoping to edit PDFS and make arrangements for the signature and I get to do it with my current plan. Can I ask to sign up for the correct adobe?  I don't know what the creative cloud is all about

    I think I bought the wrong to adobe. I was hoping to edit PDFS and make arrangements for the signature and I get to do it with my current plan. Can I ask to sign up for the correct adobe?  I don't know what the creative cloud is all about

    Look at desktop applications Adobe Creative Cloud | Adobe Creative Cloud to see what is in the cloud and click on the names to find out what each

    Acrobat is what you use to create and edit a PDF file

    If that's all you want to do, you cancel your current subscription and then buy the correct subscription

    This is an open forum, not Adobe support... below to connect with Adobe personnel to help

    While the forums are open 24/7 you can't contact Adobe support at any time

    Chat support: Mon - Fri 05:00-19:00 (US Pacific Time)<=== note="" days="" and="">

    Don't forget to stay signed with your Adobe ID before accessing the link below

    Creative cloud support (all creative cloud customer service problems)

    http://helpx.Adobe.com/x-productkb/global/service-CCM.html

  • Fill in the missing dates with level

    Think im a bad thing. I have some data that missing dates (days) and I want to just fill in the missing days with 0

    This sql is to look right or I'm doing something wrong:

    SELECT H.URL, H.V_DT, NVL CNT (P.CNT, 0))

    SELECT CAL. URL, CAL. MIN_DATE + LEVEL AS V_DT OF)

    SELECT the url, MIN (D) MIN_DATE, max (D) MAX_DATE

    USER_LOG_TMP2_VW where the url is not null and current_url (select distinct URL of USER_LOG_TMP_VW)

    GROUP BY URL

    After MAX (D) > MIN (D)) CAL

    CONNECT IN CAL. MIN_DATE + LEVEL < CAL. MAX_DATE

    -GROUP BY URL, CAL. MIN_DATE + level

    ): LEFT JOIN USER_LOG_TMP2_VW P ON P.URL = H.URL AND F.F. = H.V_DT

    There are only about 75 unique URL and when I run the present it works but I can't count because there are so many lines when it should be, perhaps a few thousand. the oldest date of min goes back only a few months.

    im getting strange results. As if I add a specific url like this (below)... it works perfectly for this url. But when I go out as above where it captures all urls... it gives me weird super results. As the dates twice for duplicate URLS.

    SELECT H.URL, H.V_DT, NVL CNT (P.CNT, 0))

    SELECT CAL. URL, CAL. MIN_DATE + LEVEL AS V_DT OF)

    SELECT the url, MIN (D) MIN_DATE, max (D) MAX_DATE

    OF USER_LOG_TMP2_VW, where url is not null and current_url to (select url from USER_LOG_TMP_VW where url = 'http://testing123.com/test/test"")

    GROUP BY URL

    After MAX (D) > MIN (D)) CAL

    CONNECT IN CAL. MIN_DATE + LEVEL < CAL. MAX_DATE

    -GROUP BY URL, CAL. MIN_DATE + level

    ): LEFT JOIN USER_LOG_TMP2_VW P ON P.URL = H.URL AND F.F. = H.V_DT

    I was not post create it for the views with her but I think that maybe im just something wrong with the level.

    I tried the group according to the level of url cal.min_date +, but for some reason it just keeps running and the query never ends.

    Hello

    Depending on your needs and your version, you may want a CONNECT BY clause like this:

    CONNECT BY LEVEL< max_date="" +="" 1="" -="">

    AND PRIOR url = url

    AND PRIOR SYS_GUID () IS NOT NULL

  • I have been hacked and you will have to reset my PC to factory setting and reinstall everything.  To get my Adobe Lightroom rear 6 where she was re - download the software and then Exchange with my current backup catalog catalog.  Will this work?  I have

    I have been hacked and you will have to reset my PC to factory setting and reinstall everything.  To get my Adobe Lightroom rear 6 where she was re - download the software and then Exchange with my current backup catalog catalog.  Will this work?  -What I else I need / have to do?

    Yep, it shud work.

    https://helpx.Adobe.com/Lightroom/help/import-photos-various-sources.html

    Restore a backup catalog

    • Choose file > open catalog.

    • Navigate to the location of your backup catalog file.

    • Select the backup the .lrcat file and click Open.

    • (Optional) Copy the catalog to the location of the original catalogue to replace.

Maybe you are looking for