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}

Similar Messages

  • CONNECT BY PRIOR on 9i

    This 9i SQL Reference indicates that it is possible to combine CONNECT BY PRIOR with a join predicate.
    In 8i I get the following error:
    ORA-01437 cannot have join with CONNECT BY
    Cause: A join operation was specified with a CONNECT BY clause. If a CONNECT BY clause is used in a SELECT statement for a tree-structured query, only one table may be referenced in the query.
    Action: Remove either the CONNECT BY clause or the join operation from the SQL statement.
    I can't find documentation to test the obvious hypothesis - comments?

    My alternative solutions.
    ***Sample***
    ID  PrevID
    1    null
    2       1
    3       1
    4       3
    ***output which we want***
    ID  PrevID  isLeaf
    1    null       0
    2       1       1
    3       1       0
    4       3       1
    ***DDL***
    create table CloneIsLeaf as
    select 1 as ID,null as PrevID from dual
    union select 2,1 from dual
    union select 3,1 from dual
    union select 4,3 from dual;
    --method1
    select ID,PrevID,
    case when exists(select 1 from CloneIsLeaf b
                      where a.ID = b.PrevID) then 0 else 1 end as isLeaf
      from CloneIsLeaf a
    Start With ID = 1
    connect by prior ID = PrevID;
    --method2
    select ID,PrevID,
    case when Level < Lead(Level) over(order by RowNum)
         then 0 else 1 end as isLeaf
      from CloneIsLeaf
    Start With ID = 1
    connect by prior ID = PrevID;
    --method3
    select ID,PrevID,
    case when LV < Lead(LV) over(order by RowNum)
         then 0 else 1 end as isLeaf
    from (select ID,PrevID,Level as LV
            from CloneIsLeaf
          Start With ID = 1
          connect by prior ID = PrevID
          order siblings by ID);
    --method4
    select ID,PrevID,
    case when LV < Lead(LV) over(order by Row_Num)
         then 0 else 1 end as isLeaf
    from (select ID,PrevID,LV,RowNum as Row_Num
            from (select ID,PrevID,Level as LV
                    from CloneIsLeaf
                  Start With ID = 1
                  connect by prior ID = PrevID
                  order siblings by ID));on method2 and method3 and method3,
    I used that "Hierarchical Queries" is depth-first search (http://en.wikipedia.org/wiki/Depth-first_search)
    http://download-west.oracle.com/docs/cd/B19306_01/server.102/b14200/queries003.htm#i2053935
    my site :-)
    http://www.geocities.jp/oraclesqlpuzzle/10-149.html

  • CONNECT BY PRIOR and performance of Query Plan

    Anyone,
    I have an SQL Statement that is performing rather slow and I am trying to figure out if I could optimize it. Here is the SQL:
       SELECT/*+ index(MAXIMO.EQNDX99) */
            maximo.equipment.eqnum, maximo.equipment.parent, LEVEL
       FROM maximo.equipment@maxi_dblink
       WHERE parent = :b1 CONNECT BY PRIOR eqnum = parent
       ORDER BY eqnum, LEVELAfter some research in this board I followed some advice found to create an index on the table for both the eqnum, parent and the parent, eqnum. EQNDX99 and EQNDX999 respectivley.
    Now the Qery Plan for this query shows the following:
    SELECT STATEMENT (REMOTE)
       SORT (ORDER BY)
          FILTER
             CONNECT BY
                 INDEX (FAST FULL SCAN) EQNDX99 (NON-UNIQUE)
                 TABLE ACESS (BY USER ROWID) EQUIPMENT
                 INDEX (RANGE SCAN) EQNDX999 (NON-UNIQUE)Now it appears to be using both indexes but it is operating through a DBLINK. Is there anything else I can do to increase performance??? It appears to be using the HINT through the link as well.
    Thanks for any help I can get,
    David Miller

    how long does it takes to complete the query?

  • 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

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

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

  • Connect by prior - reverse order

    Is this possible:
    emp - manager
    a 0
    b a
    c a
    d b
    I can start with the manager = 0 and get the entire tree. I want to start with emp = c and get the tree going the other way .. reverse order. Is this possible ?

    Do you mean:
    SQL> SELECT * FROM t[
    EMP        MGR
    a          0
    b          a
    c          a
    d          b
    SQL> SELECT LPAD(' ',2*(LEVEL-1))||emp emp, mgr
      2  FROM t
      3  START WITH emp = 'c'
      4  CONNECT BY PRIOR mgr = emp;
    EMP        MGR
    c          a
      a        0TTFN
    John

  • 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

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

  • ITunes 7.2 Problem: Song Order Problem

    Equipment Specs:
    IPOD -- First Generation, 1 G size, Firmware ver # 1.1.5
    ITunes -- Ver # 7.2.0.34
    Problem
    I recently updated to the above version of ITunes. I also recently reset/erased all my songs (no podcasts or nothing else)from my IPOD.
    I then reloaded just songs to my pod using the drag and drop method from the music file. I then re-arranged the songs in my desired order.
    I then disonnected the pod and exited ITunes.
    When I later restarted ITunes with the pod connected, my song order had changed. With the pod still open in ITunes, I am no longer able to change the play order position. As say, move song #45 to position #2 and etc.
    Has something changed as I was always able to do this prior to updating ITunes and the pod ?
    Was this caused by updating ITunes to the newest version?
    Is there a solution to be able to manage the order of your songs as before ?
    Can I use a 6.0 version of Itunes inorder to regain this song order problem. I was always able to use this function in a previous version.
    Wished I never would have updated to 7.2 from whatever I had. I really should have know better.
    Thx
    Arc

    Solved my problem by reloading ITunes Version 7.1.1.5. Now I can accomplish what I want to do.
    Here's an associated thread:
    http://discussions.apple.com/thread.jspa?threadID=981696&tstart=0

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

  • Connect by prior return unnecessary rows

    Hello,
    I am using oracle 9.2
    my query looks like this:
    SELECT *
    FROM book_items t
    where cust_id = 2305240
    and sort_key is not null
    CONNECT BY PRIOR item_id = parent_id
    START WITH t.item_id = 193
    ORDER SIBLINGS BY sort_key
    the problem is, when I have a father that has no sort_key (null) then it doesn't come up in the result, BUT his child does! they have a sort key, but why do I see them if the father is not exist in the result, it makes no sense to me...?
    How can I fix this, if the father doesn't comes up, I don't need his child's?
    Thanks.

    It's documented behaviour that:
    Oracle processes hierarchical queries as follows:
    A join, if present, is evaluated first, whether the join is specified in the FROM clause or with WHERE clause predicates.
    The CONNECT BY condition is evaluated.
    Any remaining WHERE clause predicates are evaluated.The predicates "cust_id = 2305240 and sort_key is not null" are applied last.

Maybe you are looking for