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.

Similar Messages

  • 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
    {code}
    (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.

  • How to get the root node for a child using connect by prior

    Hi,
    I searched at many places to do this but not able to get the exact o/p sp posting my question here.
    I have a table with parent_id and child_id columns. The levels of this parent_child can be many but my aim is to find the ultimate parent ( dont know the right term)
    like if I have a child_node= xyz and its parent= pqr and its parents=lmn which might be ultimate parent which doesnt have any further parent.
    So if i start with child_code= "xyz" then i should get the parent as "lmn" and not the immediate parent "pqr".
    Please help.
    Thanks,
    Aashish

    To find the Parent on emp table:
    select empno,ename,level
    from emp
    where level=3
    start with empno=7934
    connect by  empno= prior mgrTo find the Child on emp table:
    select empno,ename,level
    from emp
    where level=1
    start with empno=7934
    connect by prior empno=  mgrBut you need to know the level or position to specify the level of parent or child. But there are some other possible options available if we can know the requirement properly.
    Edited by: Vasista on Jan 11, 2011 2:27 AM
    Edited by: Vasista on Jan 11, 2011 2:30 AM

  • 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 (hierarchical query)

    Hi All,
    Some1 asked me a question which goes like this:
    Source
    emp_no dep_no
    110 10
    110 20
    110 30
    110 40
    120 10
    130 10
    130 20
    write a query to achieve the following output from the above source
    emp_no dept_no
    110 10203040
    120 10
    130 1020
    Now I have come across solutions with 'connect by prior' clauses but I am nt able to understand how oracle is producing that result , Could someone point me to a good article or text that can explain this concept very thoroughly( I have searched on google and got to see many articles but I couldnt able to understand it since these articles were not explaining everything)
    Regards
    Rahul

    You can try this:
    SQL> ed
    Wrote file afiedt.buf
      1  SELECT deptno,MAX(TRIM(SUBSTR(SYS_CONNECT_BY_PATH(empid,','),2)))
      2         KEEP(DENSE_RANK LAST ORDER BY deptno) FROM
      3  (SELECT deptno,empid,ROW_NUMBER() OVER(PARTITION BY deptno ORDER BY deptno) curr,
      4                       ROW_NUMBER() OVER(PARTITION BY deptno ORDER BY deptno) - 1 prev
      5  FROM emp)
      6  CONNECT BY PRIOR curr = prev
      7  AND deptno = PRIOR deptno
      8  START WITH curr = 1
      9* group by deptno
    SQL> /
        DEPTNO MAX(TRIM(SUBSTR(SYS_CONNECT_BY_PATH(EMPID,','),2)))KEEP(DENSE_RANKLASTORDERBYDEPTNO)
            10 1,2,3,4
            20 5,6,7
    SQL> select deptno,empid from emp;
        DEPTNO      EMPID
            10          1
            10          2
            10          3
            10          4
            20          5
            20          6
            20          7
    7 rows selected.
    SQL>

  • Connect BY, Prior, Start clause

    I want to understand how does the Connect BY, Prior, Start clause work, i mean the concept.
    Please explain with an example.
    I hope, my question is clear. Please help in solving the doubt as it is urgent.
    Regards.

    Here is the simple example you can find in a dept first traversal fashion the records come in mgr-emp hirarchy
    SQL>SELECT * FROM EMP
      2  START WITH MGR IS NULL
      3  CONNECT BY PRIOR EMPNO = MGR;
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTN
          7839 KING       PRESIDENT            17-NOV-81       5000                    1
          7566 JONES      MANAGER         7839 02-APR-81       2975                    2
          7788 SCOTT      ANALYST         7566 19-APR-87       3000                    2
          7876 ADAMS      CLERK           7788 23-MAY-87       1100                    2
          7902 FORD       ANALYST         7566 03-DEC-81       3000                    2
          7369 SMITH      CLERK           7902 17-DEC-80        800                    2
          7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    3
          7499 ALLEN      SALESMAN        7698 20-FEB-81       1600                    3
          7521 WARD       SALESMAN        7698 22-FEB-81       1250                    3
          7654 MARTIN     SALESMAN        7698 28-SEP-81       1250                    3
          7844 TURNER     SALESMAN        7698 08-SEP-81       1500                    3
          7900 JAMES      CLERK           7698 03-DEC-81        950                    3
          7782 CLARK      MANAGER         7839 09-JUN-81       2450                    1
          7934 MILLER     CLERK           7782 23-JAN-82       1300                    1

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

  • To remove  duplicate  data using connect by prior

    Hi ,
    I want to details of the employee whom reporting to without duplication .
    In table data,one employee reporting to two employees.so that reporting to process is coming two times.
    Query:
    SELECT lpad(' ', (level - 1) * 2) || EMPLOYEE_NAME as EMP_NAME,SUP_BU AS BU_CODE,SUP_REP_BU,EMP_NO,EMPLOYEE_NAME,LEVEL AS THE_LEVEL
    FROM ATTD_REPORT_TO_VW
    WHERE EMP_NO IS NOT NULL
    CONNECT BY PRIOR SUP_BU = SUP_REP_BU
    START WITH SUP_BU = :p_bu
    BUT i get the duplicate data,SUPPOSE i remove the duplication using distinct keyword ,the order of hierarchical is going wrong.
    Pls provide the solution.
    Thanks ,
    Maran

    plz ask this question in seperate SQL/PLSQL forum and also provide more information with sample data

  • SQL with connect by prior running for a long time

    Hi,
    We are using Oracle 10g. Below is a cursor sql which is having performance issues. The pAccountid is being passed from the output of a different cursor. But this cursor sql is running for a long time. Could you please help me in tuning this sql. I believe the subquery with connect by prior is causing the trouble.
    The TRXNS is a huge table which is not partitioned. The query is forced to use the index on the accountid of the TRXNS table.
    The accountlink table has 20,000 records and the TRXNStrack table has 10,000 records in total.
    This sql executes for 200,000 pAccountids and runs for more than 8 hours.
    SELECT /*+ INDEX(T TRXNS_ACCOUNTID_NIDX) */ AL.FROMACCOUNTID oldaccountid ,
                                    A.ACCOUNTNUM  oldaccountnum,
                                   T.TRXNSID,
                                   T.TRXNSTYPEID,
                                   T.DESCRIPTION ,
                                   T.postdt,
                                   T.TRXNSAMT
                        FROM
                        ACCOUNTLINK AL,
                        TRXNS T,
                        ACCOUNT A
                       WHERE AL.TOACCOUNTID IN
                                                             (SELECT TOACCOUNTID FROM ACCOUNTLINK START WITH TOACCOUNTID = pAccountid
                                                                                                                         CONNECT BY PRIOR FROMACCOUNTID  = TOACCOUNTID)
                            AND AL.FROMACCOUNTID = T.ACCOUNTID
                            AND A.ACCOUNTID = AL.FROMACCOUNTID
    AND NOT EXISTS (select 1 from TRXNStrack trck where trck.TRXNSid = t.TRXNSid AND TRXNSTrackReasonid = 1)
                            AND T.postdt > A.CLOSEDATE
                            AND T.postdt >= sysdate-2
                            AND T.postdt <= sysdate;
    Create script for trxn table:
    CREATE TABLE SP.TRXNS
      TRXNSID      NUMBER(15) CONSTRAINT "BIN$rpIQEeyLDfbgRAAUT4DEnQ==$0" NOT NULL,
      ACCOUNTID    NUMBER(15) CONSTRAINT "BIN$rpIQEeyMDfbgRAAUT4DEnQ==$0" NOT NULL,
      STATEMENTID  NUMBER(15),
      TRXNSTYPEID  NUMBER(15),
      DESCRIPTION  VARCHAR2(80 BYTE),
      postdt     DATE,
      TRXNSAMT     NUMBER(12,2),
      TRXNSREQID   NUMBER(15),
      LASTUPDATE   DATE,
      SOURCEID     NUMBER(15),
      HIDE         VARCHAR2(1 BYTE)
    TABLESPACE SO_TRXN_DATA
    RESULT_CACHE (MODE DEFAULT)
    PCTUSED    40
    PCTFREE    10
    INITRXNS   2
    MAXTRXNS   255
    STORAGE    (
                INITIAL          50M
                NEXT             1M
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                FREELISTS        8
                FREELIST GROUPS  1
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
    LOGGING
    NOCOMPRESS
    NOCACHE
    NOPARALLEL
    MONITORING;
    CREATE INDEX SP.TRXNS_ACCOUNTID_NIDX ON SP.TRXNS
    (ACCOUNTID, postdt)
    LOGGING
    TABLESPACE SO_TRXN_INDEX
    PCTFREE    10
    INITRXNS   2
    MAXTRXNS   255
    STORAGE    (
                INITIAL          64K
                NEXT             1M
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                FREELISTS        1
                FREELIST GROUPS  1
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
    NOPARALLEL;
    below is the executing plan for this sql taken from toad :
    PLAN_ID
    TIMESTAMP
    OPERATION
    OPTIONS
    OBJECT_OWNER
    OBJECT_NAME
    OBJECT_ALIAS
    OBJECT_INSTANCE
    OBJECT_TYPE
    OPTIMIZER
    SEARCH_COLUMNS
    ID
    PARENT_ID
    DEPTH
    POSITION
    COST
    CARDINALITY
    BYTES
    CPU_COST
    IO_COST
    TEMP_SPACE
    ACCESS_PREDICATES
    FILTER_PREDICATES
    PROJECTION
    TIME
    QBLOCK_NAME
    1121
    9/10/2013 3:30
    FILTER
    1
    0
    1
    1
    NOT EXISTS (SELECT 0 FROM "TRXNSTRACK" "TRCK" WHERE "TRXNSTRACKREASONID"=1 AND "TRCK"."TRXNSID"=:B1)
    AL."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22], "A"."ACCOUNTNUM"[VARCHAR2,19]
    SEL$5DA710D3
    1121
    9/10/2013 3:30
    FILTER
    2
    1
    2
    1
    SYSDATE@!-2<=SYSDATE@!
    AL."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22], "A"."ACCOUNTNUM"[VARCHAR2,19]
    1121
    9/10/2013 3:30
    NESTED LOOPS
    3
    2
    3
    1
    (#keys=0) "AL"."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22], "A"."ACCOUNTNUM"[VARCHAR2,19]
    1121
    9/10/2013 3:30
    NESTED LOOPS
    4
    3
    4
    1
    5
    1
    119
    3989858
    4
    (#keys=0) "AL"."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22], "A".ROWID[ROWID,10]
    1
    1121
    9/10/2013 3:30
    NESTED LOOPS
    5
    4
    5
    1
    4
    1
    90
    3989690
    3
    (#keys=0) "AL"."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22]
    1
    1121
    9/10/2013 3:30
    HASH JOIN
    SEMI
    6
    5
    6
    1
    3
    2
    54
    3989094
    2
    AL."TOACCOUNTID"="TOACCOUNTID"
    (#keys=1) "AL"."FROMACCOUNTID"[NUMBER,22]
    1
    1121
    9/10/2013 3:30
    INDEX
    FULL SCAN
    SP
    ACCOUNTLINK_AK1
    AL@SEL$1
    INDEX (UNIQUE)
    ANALYZED
    7
    6
    7
    1
    1
    18
    252
    107
    1
    AL."FROMACCOUNTID"[NUMBER,22], "AL"."TOACCOUNTID"[NUMBER,22]
    1
    SEL$5DA710D3
    1121
    9/10/2013 3:30
    VIEW
    SYS
    VW_NSO_1
    VW_NSO_1@SEL$5DA710D3
    11
    VIEW
    8
    6
    7
    2
    2
    18
    234
    107
    1
    TOACCOUNTID[NUMBER,22]
    1
    SEL$683B0107
    1121
    9/10/2013 3:30
    CONNECT BY
    NO FILTERING WITH START-WITH
    9
    8
    8
    1
    TOACCOUNTID=PRIOR "FROMACCOUNTID"
    TOACCOUNTID=56354162
    TOACCOUNTID[NUMBER,22], "FROMACCOUNTID"[NUMBER,22], PRIOR NULL[22], LEVEL[4]
    SEL$683B0107
    1121
    9/10/2013 3:30
    INDEX
    FULL SCAN
    SP
    ACCOUNTLINK_AK1
    ACCOUNTLINK@SEL$3
    INDEX (UNIQUE)
    ANALYZED
    10
    9
    9
    1
    1
    18
    252
    107
    1
    ACCOUNTLINK.ROWID[ROWID,10], "FROMACCOUNTID"[NUMBER,22], "TOACCOUNTID"[NUMBER,22]
    1
    SEL$3
    1121
    9/10/2013 3:30
    TABLE ACCESS
    BY INDEX ROWID
    SP
    TRXNS
    T@SEL$1
    2
    TABLE
    ANALYZED
    11
    5
    6
    2
    1
    1
    63
    298
    1
    T."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22]
    1
    SEL$5DA710D3
    1121
    9/10/2013 3:30
    INDEX
    RANGE SCAN
    SP
    TRXNS_ACCOUNTID_NIDX
    T@SEL$1
    INDEX
    ANALYZED
    2
    12
    11
    7
    1
    1
    1
    224
    1
    AL."FROMACCOUNTID"="T"."ACCOUNTID" AND "T"."POSTDT">=SYSDATE@!-2 AND "T"."POSTDT"<=SYSDATE@!
    T.ROWID[ROWID,10], "T"."POSTDT"[DATE,7]
    1
    SEL$5DA710D3
    1121
    9/10/2013 3:30
    INDEX
    UNIQUE SCAN
    SP
    ACCOUNT_PK
    A@SEL$1
    INDEX (UNIQUE)
    ANALYZED
    1
    13
    4
    5
    2
    1
    1
    90
    1
    A."ACCOUNTID"="AL"."FROMACCOUNTID"
    A.ROWID[ROWID,10]
    1
    SEL$5DA710D3
    1121
    9/10/2013 3:30
    TABLE ACCESS
    BY INDEX ROWID
    SP
    ACCOUNT
    A@SEL$1
    3
    TABLE
    ANALYZED
    14
    3
    4
    2
    1
    1
    29
    168
    1
    A."CLOSEDATE"<SYSDATE@! AND "T"."POSTDT">"A"."CLOSEDATE"
    A."ACCOUNTNUM"[VARCHAR2,19]
    1
    SEL$5DA710D3
    1121
    9/10/2013 3:30
    INDEX
    RANGE SCAN
    SP
    TRXNSTRACK_TRXNSID_NIDX
    TRCK@SEL$6
    INDEX
    ANALYZED
    2
    15
    1
    2
    2
    1
    1
    10
    73
    1
    TRCK."TRXNSID"=:B1 AND "TRXNSTRACKREASONID"=1
    TRCK."TRXNSID"[NUMBER,22], "TRXNSTRACKREASONID"[NUMBER,22]
    1
    SEL$6
    Please help me in debugging this thanks!

    Hi,
    Thanks for your thought on this subject. Below is the trace info that I got from the DBA
    SQL ID: d0x879qx2zgtz Plan Hash: 4036333519
    SELECT /*+ INDEX(T TRXNS_ACCOUNTID_NIDX) */ AL.FROMACCOUNTID OLDACCOUNTID ,
      A.ACCOUNTNUM OLDACCOUNTNUM, T.TRXNSID, T.TRXNSTYPEID, T.DESCRIPTION ,
      T.POSTDT, T.TRXNSAMT
    FROM
    ACCOUNTLINK AL, TRXNS T, ACCOUNT A WHERE AL.TOACCOUNTID IN (SELECT
      TOACCOUNTID FROM ACCOUNTLINK START WITH TOACCOUNTID = :B3 CONNECT BY PRIOR
      FROMACCOUNTID = TOACCOUNTID) AND AL.FROMACCOUNTID = T.ACCOUNTID AND
      A.ACCOUNTID = AL.FROMACCOUNTID AND NOT EXISTS (SELECT 1 FROM TRXNSTRACK
      TRCK WHERE TRCK.TRXNSID = T.TRXNSID AND TRXNSTRACKREASONID = :B4 ) AND
      T.POSTDT > A.CLOSEDATE AND T.POSTDT >= :B2 AND T.POSTDT <= :B1
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        0      0.00       0.00          0          0          0           0
    Execute  17160      2.10       1.87          0          0          0           0
    Fetch    17160   7354.61    7390.86     169408    5569856  883366791           0
    total    34320   7356.71    7392.74     169408    5569856  883366791           0
    Misses in library cache during parse: 0
    Optimizer mode: CHOOSE
    Parsing user id: 38     (recursive depth: 1)
    SQL ID: gs89hpavb4cts Plan Hash: 3415795327
    SELECT A.ACCOUNTID, C.MEMBERID, A.PROGRAMID, A.ACCOUNTNUM
    FROM
    CUSTOMER C, CUSTOMERACCOUNT CA, ACCOUNT A, PROGRAMPARAMVALUE PPV,
      BATCHPROCESSPROGRAM BP WHERE A.PROGRAMID = BP.PROGRAMID AND A.PROGRAMID =
      PPV.PROGRAMID AND A.ACCOUNTID = CA.ACCOUNTID AND CA.PERSONID = C.PERSONID
      AND PPV.PARAMID = :B2 AND PPV.VALUE = 'Y' AND BP.PROCESSID = :B1 AND BP.RUN
      = 'Y' AND A.ACCOUNTTYPEID = 4 AND A.ACCOUNTSTATUSID = 1 AND C.MEMBERID IS
      NOT NULL
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        0      0.00       0.00          0          0          0           0
    Execute      0      0.00       0.00          0          0          0           0
    Fetch      172     13.14     115.34      80826     278650          0       17200
    total      172     13.14     115.34      80826     278650          0       17200
    Misses in library cache during parse: 0
    Parsing user id: 38     (recursive depth: 1)
    OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        0      0.00       0.00          0          0          0           0
    Execute      0      0.00       0.00          0          0          0           0
    Fetch        0      0.00       0.00          0          0          0           0
    total        0      0.00       0.00          0          0          0           0
    Misses in library cache during parse: 0
    OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        0      0.00       0.00          0          0          0           0
    Execute  17160      2.10       1.87          0          0          0           0
    Fetch    17332   7367.75    7506.21     250234    5848506  883366791       17200
    total    34492   7369.85    7508.09     250234    5848506  883366791       17200
    Misses in library cache during parse: 0
        2  user  SQL statements in session.
        0  internal SQL statements in session.
        2  SQL statements in session.
    Trace file: svoprod_ora_12346.trc
    Trace file compatibility: 11.1.0.7
    Sort options: default
           1  session in tracefile.
           2  user  SQL statements in trace file.
           0  internal SQL statements in trace file.
           2  SQL statements in trace file.
           2  unique SQL statements in trace file.
       66499  lines in trace file.
        7516  elapsed seconds in trace file.

  • Query tuning for query using connect by prior

    I have written following query to fetch the data. The query is written in this format because there are multiple rows, which make one recrd and we need to bring that record into one row.
    For one CAT(commented here), this query takes around 4 minutes and fetches 6900 records but when it runs for 3 CAT, it takes 17 mins.
    I want to tune this as this has to run for 350 CAT values.
    It is doing FTS on the main table. I tried to use different hints like PARALLEL, APPEND (in insert) but nothing worked.
    The cost of the query is 51.
    Any help/suggestions will be appreciated.
    SELECT DISTINCT MIN(SEQ) SEQ,
    PT, APP, IT, LR, QT,CD, M_A_FLAG,
    STRAGG(REM) REM, -- aggregates the data from different columns to one which is parent
    CAT
    FROM (WITH R AS (SELECT CAT, SEQ, PT, M_A_FLAG, IT, LR,QT,CD, REM, APP
    FROM table1
    WHERE REC = '36' AND M_A_FLAG = '1'
    --AND CAT = '11113')
    SELECT CAT, SEQ,
    CONNECT_BY_ROOT PT AS PT,
    CONNECT_BY_ROOT APP AS APPL,
    M_A_FLAG,
    CONNECT_BY_ROOT IT AS IT,
    CONNECT_BY_ROOT LR AS LR,
    CONNECT_BY_ROOT QT AS QT,
    CONNECT_BY_ROOT CD AS CD,
    REM
    FROM R A
    START WITH PT IS NOT NULL
    CONNECT BY PRIOR SEQ + 1 = SEQ
    AND PRIOR CAT = CAT
    AND PT IS NULL)
    GROUP BY PT, APP, IT,LR, QT, CD, M_A_FLAG, CAT
    ORDER BY SEQ;
    Thanks.
    Edited by: user2544469 on Feb 11, 2011 1:12 AM

    The following threads detail the approach and information required.
    Please gather relevant info and post back.
    How to post a SQL tuning request - HOW TO: Post a SQL statement tuning request - template posting
    When your query takes too long - When your query takes too long ...

  • Help me in understand connect By Prior

    Hi ,
    please help me in understand connect By Prior
    I did a sample example , but unale to follow , please explain
    How did it understand that KING shuld be displaed first .
    On wht basis the results are shown here ?
    SELECT empno,
    ename,
    job,
    mgr,
    hiredate,
    level
    FROM   emp
    START WITH mgr IS NULL
    CONNECT BY PRIOR empno = mgr
    7839     KING PRESIDENT      17-Nov-81     1
    7566     JONES MANAGER 7839     2-Apr-81     2
    7788     SCOTT ANALYST 7566     19-Apr-87     3
    7876     ADAMS CLERK 7788     23-May-87     4
    7902     FORD ANALYST 7566     3-Dec-81     3
    7369     SMITH CLERK 7902     17-Dec-80     4
    7698     BLAKE MANAGER 7839     1-May-81     2
    7499     ALLEN SALESMAN 7698     20-Feb-81     3
    7521     WARD SALESMAN 7698     22-Feb-81     3
    7654     MARTIN SALESMAN 7698     28-Sep-81     3
    7844     TURNER SALESMAN 7698     8-Sep-81     3
    7900     JAMES CLERK 7698     3-Dec-81     3
    7782     CLARK MANAGER 7839     9-Jun-81     2
    7934     MILLER CLERK 7782     23-Jan-82     3

    Hi,
    user10503747 wrote:
    Hi ,
    please help me in understand connect By Prior
    I did a sample example , but unale to follow , please explain
    How did it understand that KING shuld be displaed first .
    On wht basis the results are shown here ?In a CONNECT BY query (without an ORDER BY clause), if x is an ancestor of y (that is, if x is the parent of y, or x is the parent of the parent of y, and so on), then y will be displayed after x, but before any other row that does not have x as its ancestor. (That is also the order in which ROWNUM will be assigned. This applies only to the query in which CONNECT BY is done. If you use the results set as a sub-query, the super query may cause the results to be re-arranged. An ORDER BY clause always takes precedence.)
    In a CONNECT BY query, every row in the result set either:
    (a) satisfies the START WITH condition, or
    (b) is a descendant of some row in (a); that is, some row that satifies the START WITH condition is its ancestor.
    Since ancestors always come before their descendants, the first row in the result set must be a row that satisfied the START WITH condition. In your example, the row with ename='KING' was the only row that satisfied the condition "START WITH mgr IS NULL", and all the other rows are its descendants, so the row with ename='KING' must come first, as APC said.

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

    Hi all,
    In Connect By Prior, How can i avoid, if some parent dont have child means, it should not come in the results.......
    Regards,
    Fame

    A "parent" without a child will not show up as a parent, but it may show up as a child.
    When that happens, CONNECT_BY_ISLEAF will be 1.
    So, filter on CONNECT_BY_ISLEAF = 0.select empno, ename, job, mgr from scott.emp
    where connect_by_isleaf = 0
    start with mgr is null
    connect by mgr = prior empno;
         EMPNO ENAME      JOB              MGR
          7839 KING       PRESIDENT           
          7566 JONES      MANAGER         7839
          7788 SCOTT      ANALYST         7566
          7902 FORD       ANALYST         7566
          7698 BLAKE      MANAGER         7839
          7782 CLARK      MANAGER         7839Edited by: Stew Ashton on Jan 17, 2013 1:34 PM

  • 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

Maybe you are looking for

  • Problems with new PalmDesktop6.2 and Palm TX

    I have a Palm TX that I have been using and syncing OK for over a year.  I recently installed the PalmDesktop version 6.2 and it seemed to work well.  For Christmas, I received an external disk drive and I moved all of the folders and documents from

  • BAPI for VK11 ?

    Hi, Can anybody tell me the BAPI name with simple coding to update Condition types for different material, plant, region etc for tcode VK11. thanks n kumar

  • Context.root has not been defined

    I am trying to compile .mxml file using flex builder 3. I am using BlazeDS to run it from C:\Program Files\blazeds\tomcat\webapps\BlazeDSmsgService. I also copied the WEB-INF folder at my project folder (BlazeDSmsgService) I am getting the following

  • How to put a user defined class in Web dynpro

    How and where can we create some user defined classes? For example i wanted to create some utility classs for my project. Under what folder should i create this?

  • Index Creation Error, while creating Information space

    Hi, I'm trying to create an Information Space in explorer based off of a Universe created on a Bex Query from SAP BW.  This Universe is created in Universe design tool based on BW 7.4,  gives below error, while creating report in  Web Intelligence ba