Connect by prior problem

hi friends i have table like tree which have have two different type of leaf
my tables
deparment did,name
person perid,name
treetable id, pid,did,perid--tree table one row have only did or pid if did =2 , peridhave to be nullso i generate hierarchial structure between person and deparments
   id      pid       did     perid
    1     null       1        null
    2    1          null        1     
    3     2         null        2
    4     2          null       3   
    5     3          2          null
   6     5          null       4  
    7     5          null       5for example i select first deparment's person querry result must be (id=)2,3,4 not 6,7 because 6,7 below 5
can we write with connect by prior it?

Hi,
JAVAMAN2 wrote:
hi friends i have table like tree which have have two different type of leaf
my tables
deparment did,name
person perid,name
treetable id, pid,did,perid--tree table one row have only did or pid if did =2 , peridhave to be nullso i generate hierarchial structure between person and deparments
id      pid       did     perid
1     null       1        null
2    1          null        1     
3     2         null        2
4     2          null       3   
5     3          2          null
6     5          null       4  
7     5          null       5for example i select first deparment's person querry result must be (id=)2,3,4 not 6,7 because 6,7 below 5I'm not sure why you want to exclude 5, 6 and 7. Is it that you don't want any nodes (except the root) with NULL perid?
can we write with connect by prior it?Yes.
If my guess about excluding 5 and it's descendants is correct:
SELECT     *     -- or whatever you want
FROM     treetable
START WITH     pid     IS NULL
CONNECT BY     pid     = PRIOR id
     AND     depid     IS NULL
;If you need the names from the other tables, then outer-join both other tables, and use NVL to get the one name that is not NULL.

Similar Messages

  • Oracle Virtual Machine Connect by Prior Problem

    Hi Friends ,
    In my company we have virtualization. this is new in our organization.
    oracle image have been taken and put in to virtual machine.(vmware).
    after that, procedure which has "connect by prior" start to create wrong output.
    please help us on what to do to correct problem given above.
    (my oracle version 10g)

    JAVAMAN2 wrote:
    simple sample:
    SELECT ROWNUM+2000-1 AS YIL FROM DUAL CONNECT BY ROWNUM <= 2010-2005 ORDER BY ROWNUM+2000-1 DESC;output on virtual machine
    2009,2008,2007,2006,2005
    output on real world
    2010,2009,2008,2007,2006,2005If that is really the output from that query, then both your virtual machine and your "real world" are broken. On all 4 versions of Oracle I have available, I get:
    SQL> SELECT ROWNUM + 2000 - 1 AS YIL
      2  FROM DUAL
      3  CONNECT BY ROWNUM <= 2010 - 2005
      4  ORDER BY ROWNUM + 2000 - 1 DESC;
           YIL
          2004
          2003
          2002
          2001
          2000Which is what I would expect given that 2010 - 2005 = 5.
    Perhaps if you showed an actual cut and paste from a sqlplus session on each machine someone might be able to help, however, I'm inclined to agree with sb92075 that it is a data issue in your real query.
    John

  • Connect by prior problem with order by

    Hi,
    I and using connect by prior within a query on Oracle 8 and would like to order the results within the parent levels.
    All the documentation that I have read shows that in Oracle 9i there is an option to say order siblings by which looks like what I need, but this does not work on Oracle 8.
    Can anyone tell me how I can order the children within the parents without changing the tree structure?
    I have also tried SYS_CONNECT_BY_PATH and I just get an error saying that it is an invalid column name,

    Karen, see here for a dicussion on how to order the siblings in a pre-9i environment:
    http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:9212348049

  • Connect by prior by speific order problem.

    Oracle: 10.2.0.4
    I have a table containing events backup.
    i want to list the hierarchy within that backup oreder by the time_stamp for all levels.
    create table BCK_EVENTS
      bck_backup_id           NUMBER(9) default 0 not null,
      event_id                   NUMBER(15) default 0 not null,
      event_name              NVARCHAR2(100) default ' ' not null
      time_stamp               NUMBER(9) default 0 not null,
      parent_event            NUMBER(15) default 0 not null,
    add constraint BCK_EVENTS_PK primary key (BCK_EVENT_ID, EVENT_ID); // event_id is not unique can be under one or more backup id'sthis is not a tree with one root, there is more than one event in the root level (level 1).
    example:
    Event Name      Time         level   <- time is numeric but for easier reading.
    *Event A          10:00         1
       *Event C        10:30         2
         *Event B      11:17         3
    *Event H          12:10         1
         *Event J       12:10         2
         *Event M      12:21         2
    *Event Z          15:33         1
       *Event R        16:56          2
        *Event M      16:57          3
       *Event G        20:20         2What i tried was :
    select  lpad( '*', level*2 ) || event_id,event_name,time_stamp,parent_event,level from bck_events
         where bck_event_id=100031
         start with parent_event is null
        connect by prior event_id = parent_eventand there are two problems with it.
    1. it's not ordered even when i added an Index (parent_event,time_stamp) and try to hint it.
    2. it returns loads of multiple rows.
    hope it's clear enough. I thank for any help.
    Edited by: 973065 on Nov 25, 2012 8:04 AM
    Edited by: 973065 on Nov 25, 2012 9:23 AM
    Edited by: 973065 on Nov 25, 2012 9:29 AM
    Edited by: 973065 on Nov 25, 2012 9:31 AM

    Hi,
    973065 wrote:
    Oracle: 10.2
    I have a table containing events backup.
    i want to list the hierarchy within that backup oreder by the time_stamp for all levels.
    create table BCK_EVENTS
    bck_backup_id           NUMBER(9) default 0 not null,
    event_id                   NUMBER(15) default 0 not null,
    event_name              NVARCHAR2(100) default ' ' not null
    time_stamp               NUMBER(9) default 0 not null,
    parent_event            NUMBER(15) default 0 not null,
    Thanks for posting the version number and the CREATE TABLE statement. Don't forget to post INSERT statements for your sample data.
    add constraint BCK_EVENTS_PK primary key (BCK_EVENT_ID, EVENT_ID); // event_id is not unique can be under one or more backup id's
    this is not a tree with one root, there is more than one event in the root level (level 1).Is it a forest, that is, a set of trees?
    example:
    Event Name      Time         level   <- time is numeric but for easier reading.
    *Event A          10:00         1
    *Event C        10:30         2
    *Event B      11:17         3
    *Event H          12:10         1
    *Event J       12:10         2
    *Event M      12:21         2
    *Event Z          15:33         1
    *Event R        16:56          2
    *Event M      16:57          3
    *Event G        20:20         2
    That seems to be a forest, that is, every row has 0 or 1 parent, and no row is its own ancestor.
    What i tried was :
    select lpad( '*', level*2 ) || event_id,event_name,time_stamp,parent_event,level from bck_events
    where bck_event_id=100031
    start with parent_event is null
    connect by prior event_id = parent_event
    and there are two problems with it.
    1. it's not ordered even when i added an Index (parent_event,time_stamp) and try to hint it.Depending on your data, you may just need to add
    ORDER SIBLINGS BY  time_stampat the end, after the CONNECT BY clause.
    If you need rows sorted by time_stamp under their roots, but otherwise without regard to the hierarchy, then use CONNECT_BY_ROOT.
    2. it returns loads of multiple rows.Again, it depends on your data. I'll bet you need something more in the CONNECT BY clause, but I can't tell what without some sample data and an exxplanation of how you gett the results you posted from that data. The fact that bck_event_id is part of the primary key makes me suspect that maybe bck_event_id needs to be somewhere in the CONNECT BY clause, but that's just a wild guess.
    hope it's clear enough. I thank for any help.As mentioned before, see the forum FAQ {message:id=9360002}

  • Problem with connect by prior

    Hi,
    I've table TAB_MGR:
    EMPLOYEE................MANAGER
    ABCD...................ABC
    ABC.....................AB
    AB......................A
    WAXXY..Y...............WAXX
    WAXX...................WA
    WA.....................W
    I tried this query:
    select a.EMPLOYEE, b.MANAGER
    from
    (select EMPLOYEE, MANAGER, rownum-level rl, level lv
    from TAB_MGR connect by prior MANAGER = EMPLOYEE) a,
    (select EMPLOYEE, MANAGER, rownum-level rl, level lv
    from TAB_MGR connect by prior MANAGER = EMPLOYEE) b
    where a.lv=1 and a.rl=b.rl;
    output is:
    EMPLOYEE....................MANAGER
    ABCD........................ABC
    ABCD........................AB
    ABCD........................A
    ABC.........................AB
    ABC.........................A
    AB..........................A
    WAXXYY.....................WAXX
    WAXXYY.....................WA
    WAXXYY.....................W
    WAXX.......................WA
    WAXX.......................W
    WA.........................W
    but I'd like to get also the level 1 of employee = MANAGER
    In my case I'd like to get this output:
    EMPLOYEE........................MANAGER
    ABCD..........................ABCD
    ABCD..........................ABC
    ABCD..........................AB
    ABCD..........................A
    ABC...........................ABC
    ABC...........................AB
    ABC...........................A
    AB............................AB
    AB............................A
    WAXXYY........................WAXXYY
    WAXXYY........................WAXX
    WAXXYY........................WA
    WAXXYY........................W
    WAXX..........................WAXX
    WAXX..........................WA
    WAXX..........................W
    WA............................WA
    WA............................W
    in my query lacks:
    EMPLOYEE........................MANAGER
    ABCD..........................ABCD
    ABC...........................ABC
    AB............................AB
    WAXXYY........................WAXXYY
    WAXX..........................WAXX
    WA............................WA
    How can I get also this record in my query??
    Thanks!

    Hi,
    I had to add two record at the table TAB_MGR, because W and A haven't manager:
    New tab TAB_MGR is:
    EMPLOYEE................MANAGER
    ABCD...................ABC
    ABC.....................AB
    AB......................A
    WAXXYY.................WAXX
    WAXX....................WA
    WA.......................W
    W.........................
    A..........................
    I have written the query:
    select a.EMPLOYEE, b.MANAGER
    from
    (select EMPLOYEE, MANAGER, rownum-level rl, level lv
    from TAB_MGR connect by prior MANAGER = EMPLOYEE) a,
    (select EMPLOYEE, MANAGER, rownum-level rl, level lv
    from TAB_MGR connect by prior MANAGER = EMPLOYEE) b
    where a.lv=1 and a.rl=b.rl
    AND a.MANAGER IS NOT NULL
    AND b.MANAGER IS NOT NULL
    UNION ALL
    select NVL(r.EMPLOYEE,R.MANAGER),NVL(r.EMPLOYEE,R.MANAGER)
    from TAB_MGR r
    connect by prior r.EMPLOYEE = r.MANAGER
    group by r.EMPLOYEE,R.MANAGER;
    output is:
    EMPLOYEE........................MANAGER
    ABCD..........................ABCD
    ABCD..........................ABC
    ABCD..........................AB
    ABCD..........................A
    ABC...........................ABC
    ABC...........................AB
    ABC...........................A
    AB............................AB
    AB............................A
    A.............................A
    WAXXYY........................WAXXYY
    WAXXYY........................WAXX
    WAXXYY........................WA
    WAXXYY........................W
    WAXX..........................WAXX
    WAXX..........................WA
    WAXX..........................W
    WA............................WA
    WA............................W
    W............................W
    It seems run correctly, but now I have another problem:
    In my table TAB_MGR I have add a new column DESCRIPTION:
    EMPLOYEE................MANAGER.......DESCRIPTION
    A.......................................L1
    AB......................A..............L2
    ABC.....................AB.............L3
    ABCD...................ABC.............L4
    W......................................M1
    WA.......................W.............M2
    WAXX....................WA.............M3
    WAXXYY.................WAXX.............M4
    from my query I'd like to get this output:
    EMPLOYEE........................MANAGER.......DESCRIPTION
    ABCD...................................ABCD..............L1-L2-L3-L4
    ABCD...................................ABC...............L1-L2-L3-L4
    ABCD...................................AB................L1-L2-L3-L4
    ABCD...................................A.................L1-L2-L3-L4
    ABC....................................ABC...............L1-L2-L3
    ABC....................................AB................L1-L2-L3
    ABC....................................A.................L1-L2-L3
    AB.....................................AB................L1-L2
    AB.....................................A.................L1-L2
    A......................................A.................L1
    WAXXYY........................WAXXYY............M1-M2-M3-M4
    WAXXYY........................WAXX..............M1-M2-M3-M4
    WAXXYY........................WA................M1-M2-M3-M4
    WAXXYY........................W.................M1-M2-M3-M4
    WAXX..........................WAXX..............M1-M2-M3
    WAXX..........................WA................M1-M2-M3
    WAXX..........................W.................M1-M2-M3
    WA............................WA................M1-M2
    WA............................W.................M1-M2
    W............................W..................M1
    Have someone any idea of like completing my query?
    Thanks in advance!!!!

  • Problem on Connect by prior...

    Dear all,
    I have a problem that duplicated records are retrieved of the followings:
    SELECT GRPID, itemid, parentid from T_MENU_TREE
    where GRPID = 1
    and exists (select 1 from T_MENU_ROLE x
         where x.grpid = GRPID
         and x.itemcode = itemid)               
    connect by prior itemid = parentid
    start with parentid IS NULL;
    The result is:
    GRPID itemid parentid
    1 aaa A
    1 aaa A
    1 bbb A
    1 bbb A
    1 ccc A
    1 ccc A
    BUT, if amend the sql likes:
    SELECT GRPID, itemid, parentid from T_MENU_TREE
    where GRPID = 1
    connect by prior itemid = parentid
    start with parentid IS NULL
    and exists (select 1 from T_MENU_ROLE x
         where x.grpid = GRPID
         and x.itemcode = itemid)     
    There is no problem.           
    The result is:
    GRPID itemid parentid
    1 aaa A
    1 bbb A
    1 ccc A
    Does anyone know why?
    Please help and thanks in advance.

    jennisy wrote:
    SELECT GRPID, itemid, parentid from T_MENU_TREE
    where GRPID = 1
    *&gt; connect by prior itemid = parentid*
    start with parentid IS NULL
    and exists (select 1 from T_MENU_ROLE x
    where x.grpid = GRPID
    and x.itemcode = itemid)     Something isn't right. Your connect by clause states - "prior itemid = parentid".
    GRPID itemid parentid
    1 aaa A
    1 bbb A
    1 ccc AYet your resultset doesn't show that relationship being satisfied.
    Please post your actual sqlplus session and, if possible, the table structures and relevant data in them.
    isotope

  • Connect by prior subquery - performance problem

    Hello,
    I have some data which is organized in a folder tree. The requeriement is to be able to search from any subfolder and down.
    /Documents
    ___Folder A
    ______Doc A
    ______Doc B
    ___Folder B
    ______Doc C
    ______Doc D
    The folder structure is defined in a table called CORNERS where the records(=folders) has a ID/PARENTID relationsship to describe the folder structure.
    Another table, called MASTER, contains the main content. Each item has a CORNERID value which defined in which subfolder the document is located.
    MASTER
    ID CORNERID TITLE INDEX_URL
    100 2 Doc A http://xxx/yy.com
    101 2 Doc B http://xxz/yy.com
    102 3 Doc C http://xyz/yy.com
    103 3 Doc D http://xyz/zz.com
    CORNERS
    ID PARENTID NAME
    1 Documents
    2 1 Folder A
    3 1 Folder B
    MASTER table has ~50000 records
    CORNERS has ~900 records.
    Analyzed nighly and stats are fresh.
    Indexes defined:
    CORNERS_ID_PARENT_IDX corners(id,parentid)
    CORNERS_PARENT_ID_IDX corners(parentid,id)
    MASTER_ID_CORNERID_IDX master(id,cornerid)
    MASTER_CORNERID_ID_IDX master(cornerid,id)
    Oracle Text index (URL based) on MASTER.INDEX_URL
    Foreign key defined:
    MASTER.CORNERID references CORNERS.ID
    If I do a search without involving the hierarchy, then the search runs pretty fast:
    SQL> SELECT COUNT(*) FROM (SELECT a.id, a.cornerid FROM MASTER a WHERE (CONTAINS(title,'$ADS AND {S} AND $PARAMETER',2) > 1 OR CONTAINS(index_url,'$ADS AND {S} AND $PARAMETER',1) > 1) );
    COUNT(*)
    5125
    Elapsed: 00:00:00.14
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE (Cost=1354 Card=1 Bytes=15
    8)
    1 0 SORT (AGGREGATE)
    2 1 TABLE ACCESS (BY INDEX ROWID) OF 'MASTER' (Cost=1354 Car
    d=758 Bytes=119764)
    3 2 BITMAP CONVERSION (TO ROWIDS)
    4 3 BITMAP OR
    5 4 BITMAP CONVERSION (FROM ROWIDS)
    6 5 SORT (ORDER BY)
    7 6 DOMAIN INDEX OF 'MASTER_TITLE_IDX' (Cost=470)
    8 4 BITMAP CONVERSION (FROM ROWIDS)
    9 8 SORT (ORDER BY)
    10 9 DOMAIN INDEX OF 'MASTER_IDX' (Cost=650)
    Statistics
    1462 recursive calls
    0 db block gets
    5507 consistent gets
    347 physical reads
    0 redo size
    380 bytes sent via SQL*Net to client
    503 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    2 sorts (memory)
    0 sorts (disk)
    1 rows processed
    SQL>
    BUT, if I add a subquery to limit the search to a certain folder tree (which includes ~200 nodes), then the performance is really badly affected. The subquery itself runs fast - around 0.07 seconds, but together with the rest of the query the preformance is really bad:
    SQL> SELECT COUNT(*) FROM (SELECT a.id, a.cornerid FROM MASTER a WHERE (CONTAINS(title,'$ADS AND {S} AND $PARAMETER',2) > 1 OR CONTAINS(index_url,'$ADS AND {S} AND $PARAMETER',1) > 1) AND cornerid IN ( SELECT ID FROM corners START WITH id = 2434 CONNECT BY PRIOR id = parentid) );
    COUNT(*)
    942
    Elapsed: 00:00:01.83
    Execution Plan
    0 SELECT STATEMENT Optimizer=CHOOSE (Cost=118 Card=1 Bytes=175
    1 0 SORT (AGGREGATE)
    2 1 TABLE ACCESS (BY INDEX ROWID) OF 'MASTER' (Cost=19 Card=
    1 Bytes=162)
    3 2 NESTED LOOPS (Cost=118 Card=8 Bytes=1400)
    4 3 VIEW OF 'VW_NSO_1' (Cost=2 Card=6 Bytes=78)
    5 4 SORT (UNIQUE)
    6 5 CONNECT BY (WITH FILTERING)
    7 6 NESTED LOOPS
    8 7 INDEX (UNIQUE SCAN) OF 'SYS_C002969' (UNIQUE
    ) (Cost=1 Card=1 Bytes=4)
    9 7 TABLE ACCESS (BY USER ROWID) OF 'CORNERS'
    10 6 NESTED LOOPS
    11 10 BUFFER (SORT)
    12 11 CONNECT BY PUMP
    13 10 INDEX (RANGE SCAN) OF 'CORNERS_PARENT_ID_IDX
    ' (NON-UNIQUE) (Cost=2 Card=6 Bytes=48)
    14 3 INDEX (RANGE SCAN) OF 'MASTER_CORNERID_ID_IDX' (NON-
    UNIQUE) (Cost=1 Card=38)
    Statistics
    29267 recursive calls
    0 db block gets
    55414 consistent gets
    140 physical reads
    0 redo size
    380 bytes sent via SQL*Net to client
    503 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    12 sorts (memory)
    0 sorts (disk)
    1 rows processed
    I've tried an alternative syntax, instead of the IN clause like this:
    SELECT COUNT(*) FROM (
    WITH folders AS (
    SELECT ID
    FROM CORNERS
    START WITH ID=2434
    CONNECT BY PRIOR ID= PARENTID
    SELECT a.id
    FROM MASTER a, folders b
    WHERE a.cornerid = b.id
    AND CONTAINS(index_url,'$ADS AND {S} AND $PARAMETER',1) > 1);
    It does runfaster, but still takes around 1 second.
    Any suggestion on how to make this run faster!?
    Thanks in advance!
    -Mats

    how long does it takes to complete the query?

  • Problem Creating a query for a hierarchical tree. [using connect by prior]

    Hi all,
    I have 2 tables.
    box (box_id, box_name)
    item(item_id, item_name, box_id)
    In a box there are several items.
    I want to create a hierachical tree to display items that are present in each box.
    LIKE:
    |---BOX1
    | |----ITEM 1
    | |----ITEM 2
    |
    |---BOX2
    | |----ITEM 1
    | |----ITEM 2
    Currently i am trying this query:
    SELECT -1 state, box_name, 'icon' icon, box_id val
    from box b, item i;
    I don't know what value to put for level, i don't know how to code the 'connect by prior' part.
    Could you please advise me?
    Michaël.
    PS. Then i will eventually use this query in forms builder.

    Note the name of this forum is "SQL Developer *(Not for general SQL/PLSQL questions)*" - so only for issues with the SQL Developer tool. Please post these questions under the dedicated SQL And PL/SQL forum.
    Regards,
    K.

  • Connect by Prior in Oracle 8i question

    Need help with building tree-structured query in Oracle 8i (Forms 6i front end if it matters).
    Sample structure and data:
    CREATE TABLE table_list
    my_code NUMBER,
    my_level NUMBER,
    my_description VARCHAR2(60)
    CREATE TABLE table_content
    my_code NUMBER,
    term_code NUMBER,
    term_category VARCHAR2(5)
    INSERT into table_list values (101, 1, 'building');
    INSERT into table_list values (102, 2, 'flat');
    INSERT into table_list values (103, 3, 'living room');
    INSERT into table_list values (104, 3, 'bedroom');
    INSERT into table_list values (105, 3, 'bathroom');
    commit;
    INSERT into table_content values (101, 102, 'Sub');
    INSERT into table_content values (102, 103, 'Sub');
    INSERT into table_content values (102, 104, 'Sub');
    INSERT into table_content values (102, 105, 'Sub');
    commit;
    Need to display data in the following order:
    101 'building' --level one
         102 'flat' --level two
              105 'bathroom' --level three
              104 'bedroom' --level three
              103 'living room' --level three
    *(note alphabetical order in level three)*
    Looks like Oracle 8i does not support table joins for CONNECT BY PRIOR. Please advise!

    And you are correct, it does not display level 1. Do
    you think this is this a database structure problem
    or it could be corrected via query modification?No and yes. It's not a "structure" problem, its a data problem (as I explained above). You always fix data problems by making queries overly complex (hint, hint, I don't suggest doing it this way).
    select tl.my_level, tl.my_description , LPAD(' ',3*(my_level-1)) || substr(tl.my_description, 1, 30) description
    from table_list tl,
    (select term_Code from table_content
    start with my_code in (select my_code from table_list where my_level=1)
    connect by prior term_code = my_code
    ) x
    where x.term_code = tl.my_code
    union all
    select my_level, my_description
    from table_list
    where my_level = 1
    order by 1, 2
    better to add a row to table_content with my_code=null, term_code=101.
    otherwise you'll be pulling stupid nonsense like above with every query from here on out.

  • How to use simple SQL instead of Connect By Prior

    Currently, I am using "connect by prior" query in the application, but for reason I don�t want to use this connect by query so can any one please tell how does I get the same result by using SQL, I tried this by using procedure but unable to get the same result, specially LEVEL of the tree.
    So please tell, how would I get the correct data.
    Thanks in advance,
    AMIT.

    Hi,
    Whenever you have a question, it helps to post:
    (1) The version of Oracle (and any other relevant software) you're using
    (2) A little sample data (just enough to show what the problem is) from all the relevant tables
    (3) The results you want from that data
    (4) Your best attempt so far (formatted) I don't believe the unformated code you posted is what you're really running, since it has a syntax error ("... WHERE START WITH ..."). Please post code that really works with the sample data you posrted.
    (5) The full error message (if any), including line number
    Executable SQL statements (like "CREATE TABLE AS ..." or "INSERT ..." statements) are best for (2).
    If you can present your problem using commonly available tables (for example, scott.emp, which contains a hierarchy), then you can omit (2).
    Formatted tabular output is okay for (3). Type these 6 characters
    &#123;code&#125;
    (small letters only, inside curly brackets) before and after the tabular text, to preserve spacing.
    As Alex said, why don't you want to use CONNECT BY?
    Are you getting the correct results now, but just looking for a different way of getting them?
    Depending on your exact requirements, you could write a PLSQL function that mimics LEVEL. Don't expect it to be fast.
    Nested Sets is a completely different way of modeling trees.
    Some things are much easier with Nested Sets than they are using the Adjacency Model (the parent-child model that uses CONNECT BY).
    But some things are much harder with Nested Sets, and LEVEL is one of them.

  • Connect by prior in Oracle 11g

    I am upgrading an application from 9i to 11g and I've been told that the connect by prior sql is broken. While I am waiting for the example and the error message, is 11g pickier about looping than 9i was? Can someone point me to some documentation on 10 or 11 changes to hierarchical queries?
    Thanks in advance.
    Jim

    Hi, Jim,
    user509659 wrote:
    I am upgrading an application from 9i to 11g and I've been told that the connect by prior sql is broken. What do you mean? Post a link, or at least a more complete description of the problem.
    While I am waiting for the example and the error message, is 11g pickier about looping than 9i was? Can someone point me to some documentation on 10 or 11 changes to hierarchical queries?There were a lot of improvements to CONNECT BY in Oracle 10, including CONNECT BY NOCYCLE (for working with loops), the CONNECT_BY_ROOT operator, and pseudo-columns CONNECT_BY_ISCYCLE and CONNECT_BY_ISLEAF. Look them up in the SQL Language manual for your version.
    I don't know of any improvements or changes to CONNECT BY in Oracle 11.
    Oracle 11.2 has recursive WITH clauses, that can do everything that CONNECT BY does, and more. Some things in 11.2 are simpler and more efficient using CONNECT BY, others are better done using recursive WITH clauses.
    Everything that CONNECT BY did in earlier versions, it does in Oracle 11, as well. If you have a CONNECT BY query that works in Oracle 9, it should work in Oracle 11, without any changes, though there might be better ways to get the same results in Oracle 11.
    Whenever you have a question, post youir code, and whatever is necessary to run it, including CREATE TABLE and INSERT statments for a little sample data. Post the results you want from that sample data, and an explanation of how you get those results from that data.
    Always say which version of Oracle you're using, e.g. 11.2.0.2.0. The difference between 11.1 and 11.2 may be very significant in this case.
    See the forum FAQ {message:id=9360002}

  • CONNECT BY PRIOR statement issue

    Hi,
    I'm using the below select query to build a relationship b/w ID & parent_id, but HANA studio is throwing a syntax error. I don't see a problem around the syntax. Please throw some light's here.
    SELECT * FROM "SIVA_TEST"."Core_Competency" START with  "Competency_ID"=1
    CONNECT BY PRIOR "Competency_ID" = "Parent_Competency_ID"   ;
    Error:
    Could not execute 'SELECT * FROM "SIVA_TEST"."Core_Competency" START WITH "Competency_ID"='1' CONNECT BY PRIOR ...' in 170 ms 563 µs .
    SAP DBTech JDBC: [257] (at 46): sql syntax error: incorrect syntax near "START": line 2 col 1 (at pos 46)
    Table struct:
    create column table "SIVA_TEST"."Core_Competency"("Competency_ID" integer,
    "Competency_Name" varchar(200),
    "Competency_description" varchar(1000),
    "Parent_Competency_ID" integer,
    "start_date" date,
    primary key("Competency_ID"));
    Thanks,
    Siva.

    Insert statement for the corresponding table..
    INSERT INTO "SIVA_TEST"."Core_Competency" VALUES (1,'value 1',null,null,'2013-12-02');
    INSERT INTO "SIVA_TEST"."Core_Competency" VALUES (2,'value 2',null,1,'2013-07-31');
    INSERT INTO "SIVA_TEST"."Core_Competency" VALUES (3,'value 3',null,1,'2013-02-01');
    INSERT INTO "SIVA_TEST"."Core_Competency" VALUES (4,'value 4',null,2,'2013-06-24');
    INSERT INTO "SIVA_TEST"."Core_Competency" VALUES (5,'value 6',null,2,'2013-04-09');
    INSERT INTO "SIVA_TEST"."Core_Competency" VALUES (6,'value 7',null,3,'2013-09-12');
    INSERT INTO "SIVA_TEST"."Core_Competency" VALUES (7,'value 8',null,3,'2013-08-09');
    INSERT INTO "SIVA_TEST"."Core_Competency" VALUES (8,'value 9',null,7,'2013-04-09');
    INSERT INTO "SIVA_TEST"."Core_Competency" VALUES (9,'value 10',null,7,'2013-12-09');
    INSERT INTO "SIVA_TEST"."Core_Competency" VALUES (10,'value 11',null,4,'2013-09-08');
    INSERT INTO "SIVA_TEST"."Core_Competency" VALUES (11,'value 12',null,4,'2013-10-10');

  • CONNECT BY PRIOR question

    Hi, I have following problem
    Table TMP contains following data
    ID PID
    1
    2 1
    3 2
    select t.id
    from tmp t
    where level=(select max(level)
    from tmp t2
    connect by prior t2.pid=t2.id
    start with t2.id=&ID)
    connect by prior t.pid=t.id
    start with t.id=&ID
    Select returns 1 when ID=3
    Is there a way to write select so that it returns value for first node
    and value of ID used in select?
    Something like
    select value_of_ID
    ,t.id
    from tmp t
    start with t.id=&ID
    so I could use group by on first column (group by value_of_ID)
    Following select would not satisfy:
    select &ID
    ,t.id
    from tmp t
    start with t.id=&ID

    I would suggest something more straightforward:
    select :startnode, id
      from tmp
    where pid is null
    start with id = :startnode
    connect by prior pid = id;The WHERE clause will limit the result to just the top level - no need for a subquery or aggregate functions.

  • Connect by prior in disco

    Hi
    I have a table in Oracle which is a recursive (pigs ear) hierarchy. I want to be able to use this in disco and "wander" down the hierarchies using connect by prior and start with. Can this be done?

    Not sure if you're referring to a specific table.
    However, I have create a report in the past that simply pointed to a database view we created that used the connect by idea and all worked well.
    So, if you have a database view that returns what you're needing then no problem.

  • CONNECT BY PRIOR - ORDERING ROWS

    Hi all,
    I am working on database server - oracle 10g enterprise edition R2,
    forms - Oracle forms 10G -Version 10.1.2.0.2 on windows 2000 professional.
    I have a table ACCOUNT_GROUP_MASTER with the following structure.
    DESC ACCOUNT_GROUP_MASTER
    Name Null? Type
    ACCOUNT_GROUP_CODE NOT NULL NCHAR(6)
    ACCOUNT_GROUP_NAME NOT NULL NVARCHAR2(40)
    ACCOUNT_GROUP_TYPE CHAR(1)
    GROUP_DISP_ORDER NUMBER(3)
    MAIN_GROUP_CODE NCHAR(6)
    Account_group_code is the primary key
    and Main_group_code is the foreign key(self referencing) referring Account_group_code of the same table.
    My query and the returned data are given below.
    SELECT
    level, account_group_code act, GROUP_DISP_ORDER ord
    FROM ACCOUNT_GROUP_MASTER
    CONNECT BY PRIOR ACCOUNT_GROUP_CODE=MAIN_GROUP_CODE
    START WITH main_GROUP_code is null
    order by account_group_code
    LEVEL ACT      ORD
    1 4000 1
    2 4100
    2 4200 3
    2 4300 1
    1 2000 2
    2 2100     2
    2 2200     1
    My task is to sort the output in the order of
    First sort column is "level" and within the "level", data should be sorted by the
    group_disp_order column.
    That is the above output should come as given below.
    LEVEL ACT ORD
    1 4000 1
    2 4100
    2 4300 1
    2 4200 3
    1 2000 2
    2 2200     1
    2 2100     2
    Can anybody help me please.
    Thanks in advance.
    Regards
    Mohan

    Hi All
    The solution to the above problem is solved by the following query.
    SELECT 1,level, account_group_code|| account_group_NAME,NULL,
    account_group_code
    FROM ACCOUNT_GROUP_MASTER
    CONNECT BY PRIOR ACCOUNT_GROUP_CODE=MAIN_GROUP_CODE
    START WITH main_GROUP_code is null
    order siblings by group_disp_order
    Regards
    Mohan

Maybe you are looking for