Hierarchical Query - Connect By clause

Hi
I've written a query using the Connect By clause to generate the table data in Tree structure with the appropriate levels.
But Data might not correct such that it may have data which loops around. Like Table1 is parent of Table 2 and Table 2 is parent of Table 3 and Table 3 is again Parent of Table 1. If such incorrect Data exists then I'm getting Oracle Error "ORA-01436: CONNECT BY loop in user data" which is correct.
What I need is, whenever this error message is raised I need to give my own error message instead of this Oracle error in SQL statement. Please note, I'm not using any PLSQL block, I just have one SQL statement. Appreciate any help on this subject. Thanks.

Hi,
Outside of PL/SQL, I don't know of any way to substitute your own error messages for the ones Oracle provides.
In Oracle 10 (and up), you can write a query using the CONNECT_BY_ISCYCLE pseudo-column. Using CONNECT BY NOCYCLE, the query will never actually raise the ORA-01436 error, but you can get it to display your message in cases where it would have, had you not been using CONNECT BY NOCYCLE.
Here's how to do it:
Put your real query, plus CONNECT_BY_ISCYCLE, in the WITH-clause.
Get the MAX (CONNECT_BY_ISCYCLE) from that sub-query. Let's call this max_cbi.
Do a UNION ALL of two prongs:
(1) SELECTs everything (except, pehaps, CONNECT_BY_ISCYCLE) from the sub-query, WHERE max_cbi = 0
(2) SELECTs your "error message" from dual, WHERE max_cbi = 1.
Edited by: Frank Kulash on Nov 26, 2008 2:09 PM

Similar Messages

  • Hierarchical query with where clause

    Hi,
    How can I query hierarchically a query with WHERE clause? I have a table with three fields session_id,id and root_id.
    When I try with the following query,
    select id, level from relation
    where session_id = 79977
    connect by prior id = root_id start with id = 5042;
    It gets duplicate values.
    I want the query to show in the hierarchical manner with a filter condition using WHERE clause. Please help me how can I achieve this. If you know any link that describes more about this, please send it.
    Thanks in Advance.
    Regards,
    -Parmy

    Hi Sridhar Murthy an others,
    Thanks a lot for your/the answer. It's working for me. It saved a lot of other work around without the proper knowledge of hierarchical query. Please send me any link that describes these issues in detail and also I hope as I have mentioned in the other message, same cannot be achieved on views or ( on two different tables ???)
    Any way thanks for your reply,
    It's working for me.
    With happiness,
    -Parmy

  • Hierarchical query - CONNECT BY and result in different columns.

    Hi everyone,
    I have a small question, hoping that someone can help me. As you will see, this
    is not very important. It's just me wondering if I can learn something in SQL.
    Let's suppose I have an ORGANIZATIONS table:
    CREATE TABLE ORGANIZATIONS (
      ID_ORGANISATION NUMBER(10) PRIMARY KEY,
      NAME VARCHAR2(255) NOT NULL,
      ID_PARENT_ORGANIZATION NUMBER(10)
    INSERT INTO ORGANIZATIONS(1, 'Parent of all', null);
    INSERT INTO ORGANIZATIONS(11, 'Child 11', 1);
    INSERT INTO ORGANIZATIONS(111, 'Child 111', 11);
    INSERT INTO ORGANIZATIONS(112, 'Child 112', 12);
    INSERT INTO ORGANIZATIONS(12, 'Child 12', 1);
    INSERT INTO ORGANIZATIONS(121, 'Child 121', 12);Let's also assume that we can have an undefined number of levels.
    Now, my question:
    With a query like
    SELECT LPAD(' ', LEVEL) || NAME  as conc
      FROM ORGANIZATIONS
    START WITH ID_ORGANIZATION = 1
    CONNECT BY PRIOR ID_ORGANISATION = ID_PARENT_ORGANIZATION
    ..I have a result like
    [conc]
    "Parent of all"
       "Child 11"
         "Child 111"
         "Child 112"
       "Child 12"
       ...All in one columns.
    What I'd like is a result like this one:
    [LEVEL_1]         [LEVEL_2]      [LEVEL_3]
    "Parent of all"
                      "Child 11"
                                     "Child 111"
                                     "Child 112"
                      "Child 12"
                                     "Child 121"I'd like to have this structure for the 5 first levels. Do you think this is possible or not?
    Thanks for your advice,

    user13117585 wrote:
    I'd like to have this structure for the 5 first levels. Do you think this is possible or not?Yes, something like...
    SQL> ed
    Wrote file afiedt.buf
      1  SELECT DECODE(LEVEL,1,NAME) as conc1
      2        ,DECODE(LEVEL,2,NAME) as conc2
      3        ,DECODE(LEVEL,3,NAME) as conc3
      4        ,DECODE(LEVEL,4,NAME) as conc4
      5        ,DECODE(LEVEL,5,NAME) as conc5
      6        ,DECODE(LEVEL,6,NAME) as conc6
      7  FROM ORGANIZATIONS
      8  START WITH ID_ORGANISATION = 1
      9* CONNECT BY PRIOR ID_ORGANISATION = ID_PARENT_ORGANIZATION
    SQL> /
    CONC1                CONC2                CONC3                CONC4                CONC5             CONC6
    Parent of all
                         Child 11
                                              Child 111
                         Child 12
                                              Child 112
                                              Child 121
    6 rows selected.
    SQL>Just add levels as you need them.

  • Hierarchical Query with Rollup Sum (CONNECT BY with GROUP BY ROLLUP)

    Hi all,
    Imagine the following scenario: i have an ACCOUNT table which holds accounts and their hierarchy (currently 5 levels), and a BALANCE table which holds balance records for the accounts. Only CHILD accounts (level 5) have records in the BALANCE table. Simple example:
    CREATE TABLE accounts (account_code VARCHAR2(30), parent_account VARCHAR2(30), account_desc VARCHAR2(400));
    CREATE TABLE balances (account_code VARCHAR2(30), balance_amount NUMBER(18,2));
    INSERT INTO ACCOUNTS VALUES ('TOT',NULL,'Total');
    INSERT INTO ACCOUNTS VALUES ('ANA1','TOT','General Expenses');
    INSERT INTO ACCOUNTS VALUES ('4801001','ANA1','Small Expenses');
    INSERT INTO ACCOUNTS VALUES ('4801002','ANA1','Transportation');
    INSERT INTO ACCOUNTS VALUES ('ANA2','TOT','Health Expenses');
    INSERT INTO ACCOUNTS VALUES ('4802001','ANA2','Healthcare');
    INSERT INTO ACCOUNTS VALUES ('4802002','ANA2','Facilities');
    INSERT INTO BALANCES VALUES ('4801001', 2000);
    INSERT INTO BALANCES VALUES ('4801002', 1000);
    INSERT INTO BALANCES VALUES ('4802001', 3000);
    INSERT INTO BALANCES VALUES ('4802002', 4000);What i need in this scenario is to run a hierarchical query, where for each node i compute the sum of all its children (In LEAF nodes which are the child accounts, this sum is the value in BALANCES itself). Final Result would be:
    TOT -> 10000
      ANA1 -> 3000
        4801001 -> 2000
        4801001 -> 1000
      ANA2 -> 7000
        4802001 -> 3000
        4802002 -> 4000I have tried various ways, and found out a workaround which works for a fixed amount of levels, basically it builds the hierarchy and computes the SYS_CONNECT_BY_PATH, then splits this as a regular expression and uses GROUP BY ROLLUP to compute the higher levels. Then i assemble it again, now with the computed values. Below is the example query:
    select level
        , NVL (vfinal.child_account,'TOTAL') ||' - '||
                            ( SELECT account_desc
                                FROM accounts
                               WHERE account_code = vfinal.child_acct ) account_name
         , to_char(sum_bal, 'fm999g999g999g990') as rolled_up_balance
      from
    select coalesce( princ.lvl3, princ.lvl2, princ.lvl1 ) child_acct
         , DECODE ( princ.lvl2 , NULL
                                     , NULL
                                     , DECODE ( princ.conta_lvl3, NULL
                                     , princ.conta_lvl1,princ.conta_lvl2 ) ) parent_acct
         , sum(princ.balance_amount) sum_bal
    from (
    select hier.lvl1
         , hier.lvl2
         , hier.lvl3
         , hier.parent_account
         , hier.account_code child_acc
         , bal.balance_amount
      from ( select level 
                  , sys_connect_by_path( account_code, '/' ) hierarchy_acct
                  , REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,3) lvl3
                  , REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,2) lvl2
                  , REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,1) lvl1
                  , account_code
                  , parent_account 
               from accounts acc
               where level <= 3
               start with parent_account is null
               connect by nocycle prior account = parent_account
               order siblings by parent_account
               ) hier
          , balances  bal
      where bal.cod_conta  = hier.account_code
    ) princ
    where princ.lvl1 is not null
    group by rollup ( princ.lvl1
                    , princ.lvl2
                    , princ.lvl3 )
    order by princ.conta_lvl1
           , princ.conta_lvl2
           , princ.conta_lvl3
    ) vfinal
    where child_acct is not null
    start with parent_acct is null
    connect by nocycle prior child_acct = parent_acctAll said and done, what i need is to do the same thing for infinite levels, because this query has 3 fixed levels. Do you know how can i structure a new query where, independently of the number of levels, the parent sums are all rolled up like this?
    Thanks a lot in advance! Best Regards!
    Thiago
    Edited by: Thiago on Sep 6, 2011 11:31 AM
    Edited by: Thiago on Sep 6, 2011 1:01 PM

    Hi,
    Thiago wrote:
    Hi all,
    Imagine the following scenario: i have an ACCOUNT table which holds accounts and their hierarchy (currently 5 levels), and a BALANCE table which holds balance records for the accounts. Only CHILD accounts (level 5) have records in the BALANCE table. Simple example:
    CREATE TABLE accounts (account_code VARCHAR2(30), parent_account VARCHAR2(30), account_desc VARCHAR2(400));
    CREATE TABLE balances (account_code VARCHAR2(30), balance_amount NUMBER(18,2));
    INSERT INTO ACCOUNTS ('TOT',NULL,'Total');
    INSERT INTO ACCOUNTS ('ANA1','TOT','General Expenses');
    INSERT INTO ACCOUNTS ('4801001','ANA1','Small Expenses');
    INSERT INTO ACCOUNTS ('4801002','ANA1','Transportation');
    INSERT INTO ACCOUNTS ('ANA2','TOT','Health Expenses');
    INSERT INTO ACCOUNTS ('4802001','ANA2','Healthcare');
    INSERT INTO ACCOUNTS ('4802002','ANA2','Facilities');
    INSERT INTO BALANCES ('4801001', 2000);
    INSERT INTO BALANCES ('4801001', 1000);
    INSERT INTO BALANCES ('4802001', 3000);
    INSERT INTO BALANCES ('4802001', 4000);
    Thanks for posting the CREATE TABLE and INSERT statements. Remember why you do it: so that the people who want to help you can re-create the problem and test their ideas. If the statments don't work, then they are not so useful. None of the INSERT statements you posted work: they all need a VALUES keyword. Please test those statments before you post them.
    Also, make sure that the reuslts you post correspond to the sample data you post. In your sample data, there are no rows in balances for account_codes '4801002' or '4802002'.
    I think you want something like this:
    WITH  connect_by_results      AS
         SELECT     CONNECT_BY_ROOT account_code     AS root_account_code
         ,     account_code
         FROM     accounts
                             -- NOTE: No START WITH clause
         CONNECT BY     parent_account     = PRIOR account_code
    SELECT       c.root_account_code     || ' -> '
                          || TO_CHAR (SUM (b.balance_amount))     AS txt
    FROM           connect_by_results  c
    LEFT OUTER JOIN      balances          b  ON  c.account_code = b.account_code
    GROUP BY  c.root_account_code
    ;

  • Recursive joins / how to define query using connect by clause

    Hi,
    I have a table A have 1-1 reltionship with table Employee
    Structure of table is as follows
    Table A
    id name employee_id
    100 aa 1
    200 bb 2
    300 cc 3
    400 dd 4
    500 ee 5
    Table Employee
    id parent_id
    1 null
    2 1
    3 2
    4 3
    5 1
    6 1
    I want to get all records from A table whose employee_id is equal to 2 or (recursive)decendants/child of employee with id 2 (i.e. employee with id 2,3,4 )
    i.e I want recursively join where in I get records from table A as
    id name employee_id
    200 bb 2
    300 cc 3 (because it is child of employee with id 2)
    400 dd 4 (becaue it is child of employee with id 3 which is child of 2)
    I know we can use In clause , but it will be performance wise not good.
    Can we do it in Toplink without using IN clause?
    Oracle has connect by clause, but other database might not have this caluse. So how can write a toplink query which can run on any database.
    Any help is highly appreciated.
    Thanks a lot.

    You can use TopLink's Hierarchical Query support but this only works with Oracle DB as it relies on the database to perform the query.
    --Shaun                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • ORACLE 9I의 HIERARCHICAL QUERY의 ORDER SIBLINGS BY CLAUSE

    제품 : ORACLE SERVER
    작성날짜 : 2003-10-22
    (V9I) Oracle 9i의 Hierarchical query의 ORDER SIBLINGS BY CLAUSE
    ===============================================================
    PURPOSE
    이 문서는 Oracle 9i의 new feature인 ORDER SIBLINGS BY 절을
    Hierarchical query에 사용하는 예를 통하여 특정 컬럼을 기준으로
    Ordering된 형태로 display하는 방법을 보여준다.
    Explanation & Example
    Hierarchical query를 구현할 때 ORDER BY 절을 사용하는 것은
    Oracle 7.1 버젼부터 가능한 것이었다.
    그러나, 순서대로 ordering되지 않고 특정 컬럼(emp table의 ename)을
    기준으로 ordering하기를 원한다면 <Bulletin:10373>처럼 procedure를
    작성하여야만 하였다.
    그러나, Oracle 9i 에서는 ORDER BY 절 대신에 ORDER SIBLINGS BY 절을
    사용할 수 있어 user-defined stored procedure를 만들 필요가 없게 되었다.
    1) Ordering 하기 전의 emp table의 Hierarchical query
    SQL> @a
    ename EMPNO MGR JOB
    KING 7839 PRESIDENT
    JONES 7566 7839 MANAGER
    SCOTT 7788 7566 ANALYST
    ADAMS 7876 7788 CLERK
    FORD 7902 7566 ANALYST
    SMITH 7369 7902 CLERK
    BLAKE 7698 7839 MANAGER
    ALLEN 7499 7698 SALESMAN
    WARD 7521 7698 SALESMAN
    MARTIN 7654 7698 SALESMAN
    TURNER 7844 7698 SALESMAN
    ename EMPNO MGR JOB
    JAMES 7900 7698 CLERK
    CLARK 7782 7839 MANAGER
    MILLER 7934 7782 CLERK
    14 rows selected.
    Ordering 하기 전의 a.sql 은 다음과 같다.
    col ename format a25
    col empno format 99999
    col mgr format 99999
    col job format a15
    select rpad(' ', LEVEL*5) || ename "ename", empno, mgr, job
    from emp
    start with job='PRESIDENT'
    connect by prior empno=mgr;
    2) 9i의 new feature인 Hierarchical query를 사용하여 Ordering한 경우
    SQL> @new_a
    ename EMPNO MGR JOB
    KING 7839 PRESIDENT
    BLAKE 7698 7839 MANAGER
    ALLEN 7499 7698 SALESMAN
    JAMES 7900 7698 CLERK
    MARTIN 7654 7698 SALESMAN
    TURNER 7844 7698 SALESMAN
    WARD 7521 7698 SALESMAN
    CLARK 7782 7839 MANAGER
    MILLER 7934 7782 CLERK
    JONES 7566 7839 MANAGER
    FORD 7902 7566 ANALYST
    ename EMPNO MGR JOB
    SMITH 7369 7902 CLERK
    SCOTT 7788 7566 ANALYST
    ADAMS 7876 7788 CLERK
    14 rows selected.
    Ordering하기 위해 사용한 new_a.sql 은 다음과 같다.
    col ename format a25
    col empno format 99999
    col mgr format 99999
    col job format a15
    select rpad(' ', LEVEL*5) || ename "ename", empno, mgr, job
    from emp
    start with job='PRESIDENT'
    connect by prior empno=mgr
    order siblings by ename;
    Reference Documents
    <Bulletin:10373>

    Thanks to Kendenny, Boneist and Odie.
    Got the point that "Order Siblings by clause" cannot be used with connect by query with analytical function, Thanks Kendenny.
    Yes, I now use main query and subquery, however the subquery be just "connect by" and have the all html tags added in the main query.
    The below query is working now.
    SELECT
    CASE WHEN LAG(mylevel,1,0) OVER (ORDER BY myrownum) >= mylevel THEN '<li>'
    ELSE
    CASE LEAD(mylevel) OVER (ORDER BY myrownum)
    WHEN mylevel THEN
    CASE WHEN myrownum = 1 THEN '<ul id="sidebarmenu1" '
    ELSE '<ul'
    END ||'><li>'
    ELSE
    CASE WHEN myrownum =1 THEN '<ul id="sidebarmenu1"'
    ELSE '<ul '
    END ||' ><li>'
    END
    END ||'<a href="'||
       CASE WHEN link_url IS NOT NULL THEN
          link_url||'title="'||menu_item||'"'
    ELSE '#"' END ||
    '><span>'||short_menu_item||'</span></a>'||
    CASE mylevel - LEAD(mylevel,1,1) OVER (ORDER BY myrownum)
    WHEN -1 THEN NULL
    WHEN 0 THEN '</li>'
    ELSE REPLACE(LPAD('*', myleveL-LEAD(mylevel,1,1) OVER (ORDER BY myrownum),'*'), '*','</li></ul></li>')
    END ||
    CASE WHEN LEAD(mylevel,1,0) OVER (ORDER BY myrownum) = 0 THEN '</ul>'
    ELSE NULL END unordered_List,
    menu_item, menu_id,
    above_menu_id
    FROM (
    SELECT LEVEL mylevel, ROWNUM myrownum,daevmt.*
    FROM dae_vs_my_tasks daevmt
    CONNECT BY PRIOR daevmt.menu_id = daevmt.above_menu_id
    START WITH daevmt.above_menu_id = 'TOPMENU'
    ORDER SIBLINGS BY display_order
    ) t;
    Odie, I tried altering the session for the flag, still the first query was not working.
    Thanks again all for your great time in answering me.

  • Reg : Hierarchical Query(Using Connect By)

    Hi all,
    I got the result with the hierarchical query as :
    */qxxh*
    */qxxh/jxobcbg*
    */qxxh/jxobcbg/n00wcp4*
    */qxxh/jxobcbg/n00wcp4/x000263*
    */qxxh/jxobcbg/n00wcp4/x000263/p0263*
    */qxxh/jxxocbg*
    */qxxh/jxxocbg/n00voc1*
    */qxxh/jxxocbg/n00voc1/x000589*
    */qxxh/jxxocbg/n00voc1/x000589/p0589*
    */qxxh/jxuwxxh*
    */qxxh/jxuwxxh/n00xpxf*
    */qxxh/jxuwxxh/n00xpxf/m00bxpl*
    */qxxh/jxuwxxh/n00xpxf/m00bxpl/x000522*
    */qxxh/jxuwxxh/n00xpxf/m00bxpl/x000522/p0522*
    Here I want to select only maximum path . Here I used "SYS_CONNECT_BY_PATH"
    Please let meknow how to do this ?
    Thanks in advance .
    Edited by: udeffcv on Dec 9, 2009 10:03 PM

    udeffcv wrote:
    Hi all,
    I got the result with the hierarchical query as :
    */qxxh*
    */qxxh/jxobcbg*
    */qxxh/jxobcbg/n00wcp4*
    */qxxh/jxobcbg/n00wcp4/x000263*
    */qxxh/jxobcbg/n00wcp4/x000263/p0263*
    */qxxh/jxxocbg*
    */qxxh/jxxocbg/n00voc1*
    */qxxh/jxxocbg/n00voc1/x000589*
    */qxxh/jxxocbg/n00voc1/x000589/p0589*
    */qxxh/jxuwxxh*
    */qxxh/jxuwxxh/n00xpxf*
    */qxxh/jxuwxxh/n00xpxf/m00bxpl*
    */qxxh/jxuwxxh/n00xpxf/m00bxpl/x000522*
    */qxxh/jxuwxxh/n00xpxf/m00bxpl/x000522/p0522*
    Here I want to select only maximum path . Here I used "SYS_CONNECT_BY_PATH"
    Please let meknow how to do this ?
    Thanks in advance .
    Edited by: udeffcv on Dec 9, 2009 10:03 PMwhat do you mean by maximum path?? is it...
    */qxxh/jxobcbg/n00wcp4/x000263/p0263*
    */qxxh/jxxocbg/n00voc1/x000589/p0589*
    */qxxh/jxuwxxh/n00xpxf/m00bxpl/x000522/p0522*
    is it child nodes??
    then you might like to see
    CONNECT_BY_ISLEAF pseudo column..example you can find it in below link
    http://download.oracle.com/docs/cd/B14117_01/server.101/b10759/pseudocolumns001.htm#sthref670
    Ravi Kumar

  • Too many results in hierarchically query

    Hello all,
    I'm searching for an idea to stop getting results 2, 3 and more times out of the following query
    select
    t.lvl,
    t.syswflvl,
    t.upper,
    LPAD(' ', (lvl)*8)||t.code code,
    LPAD(' ', (lvl)*8)||t.bezeichnung bezeichnung,
    t.chk,
    t.rang
    from (
    select '0' lvl, '0-'||to_char(syswftable,'0000000') syswflvl, '0- 0000000' upper, syscode code, bezeichnung, '' chk, 0 rang from wftable where (select count(wfm.syswftable) from wfm where wfm.syswftable = wftable.syswftable) > 0 union
    select '1' lvl, '1-'||to_char(syswfm,'0000000') syswflvl, '0-'||to_char(syswftable,'0000000') upper, syscode code, kurzbez bezeichnung, anzeigefilter chk, 1 rang from wfm union
    select '2' lvl, '2-'||to_char(syswfa,'0000000') syswflvl, '1-'||to_char(syswfm,'0000000') upper, syscode code, kurzbez bezeichnung, bedingung chk, rang from wfa union
    select '3' lvl, '3-'||to_char(syswfc,'0000000') syswflvl, '2-'||to_char(syswfa,'0000000') upper, syscode code, kurzbez bezeichnung, bedingung chk, rang from wfc union
    select '4' lvl, '4-'||to_char(syswfg,'0000000') syswflvl, '3-'||to_char(syswfc,'0000000') upper, syscode code, kurzbez bezeichnung, bedingung chk, rang from wfg
    ) t
    where ((t.chk not like '%and 0%'
    and trim(t.chk) not like '0%')
    or t.chk is null)
    and upper not like '%-'
    connect by nocycle prior syswflvl = upper
    order siblings by upper, syswflvl, rang
    What happens is, that I get the results from level 0 one times, from level 1 two times, from level 2 three times etc.
    What I'm try to achive is to get only the whole thing once.
    Hope you can see what my problem is ;-)
    Regards
    Carsten

    The effect of not having a start with clause in a hierarchical query is that a hierarchy is produced starting at every possible entry-point. So, you'll get the hierachy from node 1, the hierarchy from all nodes 2, the hierarchy from all nodes 3 and so one. Whether it starts with the correct one or not is not really relevant, as you already figured out that you get too much....
    So, just believe it, add a start with clause...and your problems are gone.

  • [Oracle 8i] Need help pruning branches from a hierarchical query

    My problem is that my hierarchical query seems only to trim out the values that don't meet my criteria, but still includes their children. When my query hits a record that does not meet my criteria, I want it to stop there. I've tried including the criteria in just the 'where' clause of the query, and have also put the criteria in the 'connect by' clause as well, but nothing has fixed it. Please keep in mind I'm using Oracle 8i, so I can't use some of the 'nicer' statements for hierarchical queries that they introduced in 9. I'm stuck with 'Start With...Connect By'.
    I have sample tables/data that I can post if someone needs to see that to help me, but to start with, here's my current query:
    SELECT     *
    FROM     (
              SELECT
                   LEVEL
              ,     c_bill.comp_part_nbr                     AS     c_part_nbr
              ,     (select c_part.part_desc
                   FROM part c_part
                   WHERE c_part.part_nbr=c_bill.comp_part_nbr)     AS     c_part_desc
              ,     (SELECT c_part.part_type
                   FROM part c_part
                   WHERE c_part.part_nbr=c_bill.comp_part_nbr)      AS     c_part_type
              ,     c_bill.qty_per                          AS     c_qty_per_p
              ,     c_bill.qty_per_type                     AS     c_qty_per_type
              ,     (SELECT c_part.qty_on_hand                
                   FROM part c_part
                   WHERE c_part.part_nbr=c_bill.comp_part_nbr)      AS     c_qty_on_hand
              ,     c_bill.oper_nbr                     AS     rqd_at_op
              ,     c_bill.comp_off_adj                     AS     rqd_offset
              ,     c_bill.bom_doc_nbr                     AS     p_part_nbr
              ,     (SELECT p_part.qty_on_hand
                   FROM part p_part
                   WHERE p_part.part_nbr=c_bill.bom_doc_nbr)      AS     p_qty_on_hand
              FROM
                   BILL c_bill
              WHERE
                             (c_bill.status           =      'RL')           
                        AND     (c_bill.view_code     IN      ('M','G'))     
                        AND     (c_bill.end_eff_dt     >      SYSDATE)      
                        AND     (c_bill.begn_eff_dt     <=      SYSDATE)
              START WITH c_bill.bom_doc_nbr=RPAD(?,25)
              CONNECT BY PRIOR c_bill.comp_part_nbr=c_bill.bom_doc_nbr
              AND     c_bill.view_code     IN     ('M','G')     
              AND     c_bill.status          =     'RL'
              AND      c_bill.end_eff_dt     >     SYSDATE
              AND     c_bill.begn_eff_dt     <=     SYSDATE     
         ) a
    WHERE     c_part_type = 'M'

    The outside criterion of part_type='M' isn't my problem. Where I'm actually seeing my issue rear its ugly head is in the criterion:
    (c_bill.view_code     IN      ('M','G'))What I'll have happen is that one of the children or grandchildren of the part number I'm querying for (my parameter), will be of some view code that's not 'M' or 'G'. In my sample data below, I have a level 4 part that is part of the 'H' view code, which I don't want, nor do I want it's children. However, its child is in the 'G' view code, and my query returns it anyway.
    In my sample data below, I'm assuming that the parameter = 'XYZ-100'
    CREATE TABLE part
    part_nbr     varchar(25) not null,
    part_desc     varchar(25) not null,
    part_type     char(1) not null,
    qty_on_hand     double(13,4) not null
    CONSTRAINT part_pk
    PRIMARY KEY (part_nbr),
    CONSTRAINT check_part_type
    CHECK (part_type IN ('M','P','X','Y')),
    CONSTRAINT check_qty_on_hand
    CHECK (qty_on_hand >= 0)
    CREATE TABLE bill
    row_added_ts     char(20) not null,
    bom_doc_nbr     varchar(25) not null,
    comp_part_nbr     varchar(25) not null,
    qty_per          double(9,5) not null,
    qty_per_type     char(1) not null,
    oper_nbr     char(4) not null,
    comp_off_adj     double(3,0),
    status          char(2),
    view_code     char(1) not null,
    end_eff_dt     date() not null,
    begn_eff_dt     date() not null
    CONSTRAINT bill_pk
    PRIMARY KEY (row_added_ts),
    CONSTRAINT check_qty_per_type
    CHECK (qty_per_type IN ('0','1','2','3')),
    CONSTRAINT check_status
    CHECK (status IN ('IN', 'RL')),
    );     Values for those tables:
    INSERT INTO part
    VALUES ('xyz-1', 'purchased part', 'P', 5);
    INSERT INTO part
    VALUES ('xyz-2', 'purchased part', 'P', 1);
    INSERT INTO part
    VALUES ('xyz-3', 'purchased part', 'P', 1);
    INSERT INTO part
    VALUES ('xyz-3a', 'manufactured part', 'M', 1);
    INSERT INTO part
    VALUES ('xyz-4', 'purchased part', 'P', 1);
    INSERT INTO part
    VALUES ('xyz-9-1', 'manufactured part', 'M', 0);
    INSERT INTO part
    VALUES ('xyz-9a', 'manufactured part', 'M', 0);
    INSERT INTO part
    VALUES ('raw-1', 'purchased raw material', 'P', 212);
    INSERT INTO part
    VALUES ('raw-2', 'purchased raw material', 'P', 75.5);
    INSERT INTO part
    VALUES ('XYZ-100', 'manufactured part', 'M', 0);
    INSERT INTO part
    VALUES ('(OPEN)', '(not in use)', 'Y', 0);
    INSERT INTO part
    VALUES ('XYZ-100-1', 'manufactured part', 'M', 0);
    INSERT INTO part
    VALUES ('XYZ-100-2', 'manufactured part', 'M', 1);
    INSERT INTO part
    VALUES ('XYZ-100-3', 'manufactured part', 'M', 0);
    INSERT INTO part
    VALUES ('XYZ-100-4', 'manufactured part', 'M', 2);
    INSERT INTO part
    VALUES ('XYZ-100-A', 'manufactured part', 'M', 0);
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100','xyz-1',3,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100','XYZ-100-1',1,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-1','xyz-1',2,'1','****',1,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-1','XYZ-100-2',3,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-2','xyz-2',6,'1','****',2,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-2','xyz-4',6,'1','****',2,'IN','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-2','xyz-100-3',1,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-3','xyz-3',8,'1','****',1,'RL','M','01-Jan-2050','01-Jan-2000');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-3','xyz-3a',8,'1','****',1,'RL','M','01-Jan-2000','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-3','XYZ-100-4',4,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-3','XYZ-100-A',2,'1','****',2,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008071153100150000','XYZ-100-3','(OPEN)',2,'1','****',0,'RL','E','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008071153100150000','XYZ-100-3','xyz-9-1',2,'1','****',0,'RL','H','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-4','raw-1',8.75,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008072153100150000','XYZ-100-A','raw-2',3.75,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008075911100150000','xyz-9-1','xyz-9a',1,'1','****',0,'RL','G','01-Jan-2050','01-Jan-1900');
    INSERT INTO bill
    VALUES ('2008087711100150000','xyz-9a','raw-2',3.75,'1','****',0,'RL','G','01-Jan-2050','01-Jan-1900');Sample data displayed in table format:
    --PART table (from insert statements above)
    part_nbr     part_desc          part_type     qty_on_hand
    xyz-1           purchased part          P          5
    xyz-2           purchased part          P          1
    xyz-3           purchased part          P          1
    xyz-3a           manufactured part     M          1
    xyz-4           purchased part          P          1
    xyz-9-1           manufactured part     M          0
    xyz-9a           manufactured part     M          0
    raw-1           purchased raw material     P          212
    raw-2           purchased raw material     P          75.5
    XYZ-100           manufactured part     M          0
    (OPEN)          (not in use)          Y          0
    XYZ-100-1     manufactured part     M          0
    XYZ-100-2     manufactured part     M          1
    XYZ-100-3     manufactured part     M          0
    XYZ-100-4     manufactured part     M          2
    XYZ-100-A     manufactured part     M          0
    --BILL table (from insert statements above)
    row_added_ts          bom_doc_nbr     comp_part_nbr     qty_per     qty_per_type     oper_nbr     comp_off_adj     status     view_code     end_eff_dt     begn_eff_dt
    2008072153100150000     XYZ-100          xyz-1          3     1          ****          0          RL     G          01-Jan-2050     01-Jan-1900
    2008072223100150000     XYZ-100          XYZ-100-1     1     1          ****          0          RL     M          01-Jan-2050     01-Jan-1900
    2008072411100150000     XYZ-100-1     xyz-1          2     1          ****          1          RL     M          01-Jan-2050     01-Jan-1900
    2008072459100150000     XYZ-100-1     XYZ-100-2     3     1          ****          0          RL     M          01-Jan-2050     01-Jan-1900
    2008072578100150000     XYZ-100-2     xyz-2          6     1          ****          2          RL     M          01-Jan-2050     01-Jan-1900
    2008072694100150000     XYZ-100-2     xyz-4          6     1          ****          2          IN     G          01-Jan-2050     01-Jan-1900
    2008072786100150000     XYZ-100-2     xyz-100-3     1     1          ****          0          RL     M          01-Jan-2050     01-Jan-1900
    2008072865100150000     XYZ-100-3     xyz-3          8     1          ****          1          RL     M          01-Jan-2050     01-Jan-2000
    2008073100100150000     XYZ-100-3     xyz-3a          8     1          ****          1          RL     M          01-Jan-2000     01-Jan-1900
    2008073159100150000     XYZ-100-3     XYZ-100-4     4     1          ****          0          RL     M          01-Jan-2050     01-Jan-1900
    2008073346100150000     XYZ-100-3     XYZ-100-A     2     1          ****          2          RL     M          01-Jan-2050     01-Jan-1900
    2008073478100150000     XYZ-100-3     (OPEN)          2     1          ****          0          RL     E          01-Jan-2050     01-Jan-1900
    2008073529100150000     XYZ-100-3     xyz-9-1          2     1          ****          0          RL     H          01-Jan-2050     01-Jan-1900
    2008073798100150000     XYZ-100-4     raw-1          8.75     1          ****          0          RL     M          01-Jan-2050     01-Jan-1900
    2008073811100150000     XYZ-100-A     raw-2          3.75     1          ****          0          RL     M          01-Jan-2050     01-Jan-1900
    2008075911100150000     xyz-9-1          xyz-9a          1     1          ****          0          RL     G          01-Jan-2050     01-Jan-1900
    2008087711100150000     xyz-9a          raw-2          3.75     1          ****          0          RL     G          01-Jan-2050     01-Jan-1900--What I want to get with my query (branches pruned off my tree)
    LEVEL     C_PART_NBR     C_PART_DESC          C_PART_TYPE     C_QTY_PER_P     C_QTY_PER_TYPE     C_QTY_ON_HAND     RQD_AT_OP     RQD_OFFSET     P_PART_NBR     P_QTY_ON_HAND
    1     XYZ-100-1     manufactured part     M          1          1          0          ****          0          XYZ-100          0
    2     XYZ-100-2     manufactured part     M          3          1          1          ****          0          XYZ-100-1     0
    3     xyz-100-3     manufactured part     M          1          1          0          ****          0          XYZ-100-2     1
    4     XYZ-100-4     manufactured part     M          4          1          2          ****          0          XYZ-100-3     0
    4     XYZ-100-A     manufactured part     M          2          1          0          ****          2          XYZ-100-3     0--What I actually get with my query (includes children of items that don't meet query criteria)
    LEVEL     C_PART_NBR     C_PART_DESC          C_PART_TYPE     C_QTY_PER_P     C_QTY_PER_TYPE     C_QTY_ON_HAND     RQD_AT_OP     RQD_OFFSET     P_PART_NBR     P_QTY_ON_HAND
    1     XYZ-100-1     manufactured part     M          1          1          0          ****          0          XYZ-100          0
    2     XYZ-100-2     manufactured part     M          3          1          1          ****          0          XYZ-100-1     0
    3     xyz-100-3     manufactured part     M          1          1          0          ****          0          XYZ-100-2     1
    4     XYZ-100-4     manufactured part     M          4          1          2          ****          0          XYZ-100-3     0
    4     XYZ-100-A     manufactured part     M          2          1          0          ****          2          XYZ-100-3     0
    5     xyz-9a          manufactured part     M          1          1          0          ****          0          xyz-9-1          0Edited by: user11033437 on Jul 30, 2009 7:27 AM (grammar)

  • Connect By Clause -  Major Bug in Oracle 9.0.1

    Currently I am working with Oracle 8.1.6 database,
    now we are migrating to Oracle 9.0.1.
    We are often using "Connect By" clause in our queries since our database structure is hierarchical.
    I am facing one problem with "Connect By" clause in
    Oracle 9.0.1, actually which is working properly with Oracle 8.1.6.
    I am giving the Problem.
    I have created a Table say Object_Master,
    Then I insert 3 rows in that table,
    and then I commit and try to execute one query,
    Same thing is working properly in Oracle 8.1.6 but same query is not working in Oracle 9.0.1.
    Following is the script.
    Create table Object_Master
    ObjectId Number(5),
    ObjectName Varchar2(255),
    ParentId Number(5),
    DeleteStatus Varchar2(1),
    IsSecuritySet Varchar2(1))
    Insert Into Object_Master Values (1,'john',0,'N','Y')
    Insert Into Object_Master Values (2,'ADMIN',1,'N','Y')
    Insert Into Object_Master Values (3,'ADMIN',1,'N','Y')
    Commit
    Select * from
    Object_Master
    Start with ObjectId = 1
    Connect By parentid = Prior ObjectId
    And DeleteStatus = 'N' And ( IsSecuritySet = 'N' Or ObjectName = 'ADMIN' )
    This script is working perfectly with Oracle 8.1.6 but when I try to run this sc[i]Long postings are being truncated to ~1 kB at this time.

    Hey Mitesh,
    What's going on? So many posting for the same question, and that is also not comlete. Try to post rest of your question.
    Dharmesh Patel

  • PROBLEM WITH HIERARCHICAL QUERY - PLEASE HELP

    I have got three tables :
    CREATE TABLE FIRM
    (FID INTEGER NOT NULL PRIMARY KEY,
    FNAME VARCHAR(40),
    FTYPE VARCHAR(3),
    MASTERID INTEGER );
    CREATE TABLE FACULTY
    (FAID INTEGER NOT NULL PRIMARY KEY,
    FANAME VARCHAR(40),
    FATYPE VARCHAR(3),
    MASTERID INTEGER );
    CREATE TABLE EMPLOYEE
         (EID INTEGER NOT NULL PRIMARY KEY,
    ENAME VARCHAR(20),
    ESURNAME VARCHAR(20),
         EJOB VARCHAR(3),
         MASTERID INTEGER );
    This is a hierarchical tree(or is ment to be, I,m complete rookie ) . Firm can be the root or can be slave to another firm. Faculty can be slave to firm, or to another faculty. Employee can be slave to faculty or to another employee(e.g. boss). This connections are specified by MASTERIDs.
    I need to write a procedure, which parameter would be node ID. It is meant to create a VIEW from this node as if it was a root (view of a subtree).
    I tried CONNECT BY clause but it works only on one table at a time and I have here three tables.
    I completely don,t know how to write it. Please help.

    create view hierarchy as
    select id, master_id, name from table1
    union all
    select id, master_id, name from table2
    union all
    select id, master_id, name from table3
    Then do your connect by query against hierarchy.
    It will not work in 8i (connect by on views not allowed), so you will need to materialize the view.
    Kirill

  • Hierarchical Query Duplication Issue

    Hi All,
    I am trying to create a tree structure for a "NOT SO" elegantly structured data(for tree type traversing). The below mentioned query is the sample of the data that I have in the real-time system.
    The assumption & requirements to be considered are as follows:
    1. The query with the rep data(i.e. first WITH clause query with 9 rows) is the master data that needs to be shown in hierarchical structure.
    2. The query with the group data is joined with the rep data on GROUP_ID to get the tree structure be traversing on group_id and PARENT_GROUP_ID(i.e. Second WITH clause query)
    The problem is I am getting 16 rows instead of 9(which are expected since the master rep data query has 9 rows). Some part of the Tress is getting wrongly duplicated.
    Can anybody please point to me where I am making any mistake in the way this hierarchical query is written.
    SELECT
      REP_EMAIL
    ,REP_TYPE
    ,GROUP_MGR_EMAIL
    ,GROUP_ID
    ,PARENT_GROUP_ID
    ,RPAD('*',LEVEL*10,'*')||REP_EMAIL REP_EMAIL
    -- ,RPAD('*',LEVEL*10,'*')||GROUP_MGR_EMAIL GROUP_MGR_EMAIL
    ,SYS_CONNECT_BY_PATH(REP_EMAIL, '/') "Path"
      FROM (
          SELECT * FROM (
                       WITH REP AS(
                                     SELECT '[email protected]' AS EMAIL, 'REP' AS REP_TYPE, 112 AS GROUP_ID, 112 AS PARENT_GROUP_ID FROM DUAL UNION
                                     SELECT '[email protected]', 'REP' , 112 , 112 FROM DUAL UNION
                                     SELECT '[email protected]', 'REP' , 115 , 115 FROM DUAL UNION
                                     SELECT '[email protected]', 'REP' , 115 , 115 FROM DUAL UNION
                                     SELECT '[email protected]', 'MGR' , 112 , 117  FROM DUAL UNION
                                     SELECT '[email protected]', 'MGR' , 115 , 119 FROM DUAL UNION
                                     SELECT '[email protected]', 'MGR' , 117 , 2 FROM DUAL UNION
                                     SELECT '[email protected]', 'MGR' , 119 , 2 FROM DUAL UNION
                                     SELECT '[email protected]', 'REP' , 115 , 115  FROM DUAL
                                     SELECT EMAIL AS REP_EMAIL, REP_TYPE, GROUP_ID AS REP_GROUP_ID, PARENT_GROUP_ID AS REP_PARENT_GROUP_ID
                                       FROM REP) REP
       JOIN (
             SELECT * FROM (
                         WITH GRP AS (
                                     SELECT 1 AS GROUP_ID, NULL AS PARENT_GROUP_ID, '[email protected]' AS GROUP_MGR_EMAIL FROM DUAL UNION
                                     SELECT 2 , 1 , '[email protected]' FROM DUAL UNION
                                     SELECT 50 , 2 , '[email protected]' FROM DUAL UNION
                                     SELECT 112 , 117 , '[email protected]' FROM DUAL UNION
                                     SELECT 115 , 119 , '[email protected]' FROM DUAL UNION
                                     SELECT 117 , 2 , '[email protected]' FROM DUAL UNION
                                     SELECT 119 , 2 , '[email protected]' FROM DUAL
                                    SELECT GROUP_ID,  PARENT_GROUP_ID, GROUP_MGR_EMAIL
                                      FROM GRP) GRP) GRP
       ON (REP.REP_PARENT_GROUP_ID = GRP.GROUP_ID))
        START WITH PARENT_GROUP_ID = 1
      CONNECT BY   PARENT_GROUP_ID = PRIOR GROUP_IDAny help would be really appreciated.
    Thank you,
    Warm Regards
    Goldi

    Goldi wrote:
    The problem is I am getting 16 rows instead of 9(which are expected since the master rep data query has 9 rows). Some part of the Tress is getting wrongly duplicated. I don't think it is getting wrongly duplicated...
    SQL> ed
    Wrote file afiedt.buf
      1  WITH REP AS (SELECT EMAIL AS REP_EMAIL, REP_TYPE, GROUP_ID AS REP_GROUP_ID, PARENT_GROUP_ID AS REP_PARENT_GROUP_ID
      2               FROM (
      3                     SELECT '[email protected]' AS EMAIL, 'REP' AS REP_TYPE, 112 AS GROUP_ID, 112 AS PARENT_GROUP_ID FROM DUAL UNION
      4                     SELECT '[email protected]'        , 'REP'            , 112            , 112            FROM DUAL UNION
      5                     SELECT '[email protected]'        , 'REP'            , 115            , 115            FROM DUAL UNION
      6                     SELECT '[email protected]'        , 'REP'            , 115            , 115            FROM DUAL UNION
      7                     SELECT '[email protected]'         , 'MGR'            , 112            , 117            FROM DUAL UNION
      8                     SELECT '[email protected]'        , 'MGR'            , 115            , 119            FROM DUAL UNION
      9                     SELECT '[email protected]'        , 'MGR'            , 117            , 2              FROM DUAL UNION
    10                     SELECT '[email protected]'        , 'MGR'            , 119            , 2              FROM DUAL UNION
    11                     SELECT '[email protected]'         , 'REP'            , 115            , 115            FROM DUAL
    12                    )
    13              )
    14      ,GRP AS (
    15               SELECT 1 AS GROUP_ID, NULL AS PARENT_GROUP_ID, '[email protected]' AS GROUP_MGR_EMAIL FROM DUAL UNION
    16               SELECT 2            , 1                      , '[email protected]'                   FROM DUAL UNION
    17               SELECT 50           , 2                      , '[email protected]'                   FROM DUAL UNION
    18               SELECT 112          , 117                    , '[email protected]'                     FROM DUAL UNION
    19               SELECT 115          , 119                    , '[email protected]'                    FROM DUAL UNION
    20               SELECT 117          , 2                      , '[email protected]'                    FROM DUAL UNION
    21               SELECT 119          , 2                      , '[email protected]'                    FROM DUAL
    22              )
    23  --
    24  SELECT REP_EMAIL
    25        ,REP_TYPE
    26        ,GROUP_MGR_EMAIL
    27        ,GROUP_ID
    28        ,PARENT_GROUP_ID
    29       -- ,RPAD('*',LEVEL*10,'*')||REP_EMAIL REP_EMAIL
    30       -- ,RPAD('*',LEVEL*10,'*')||GROUP_MGR_EMAIL GROUP_MGR_EMAIL
    31       -- ,SYS_CONNECT_BY_PATH(REP_EMAIL, '/') "Path"
    32  FROM   REP JOIN GRP ON (REP.REP_PARENT_GROUP_ID = GRP.GROUP_ID)
    33  --CONNECT BY PARENT_GROUP_ID = PRIOR GROUP_ID
    34* --START WITH PARENT_GROUP_ID = 1
    SQL> /
    REP_EMAIL                                                    REP GROUP_MGR_EMAIL     GROUP_ID PARENT_GROUP_ID
    [email protected]                                             REP [email protected]          112          117
    [email protected]                                             REP [email protected]         115          119
    [email protected]                                             REP [email protected]         115          119
    [email protected]                                              REP [email protected]          112          117
    [email protected]                                              REP [email protected]         115          119
    [email protected]                                             MGR [email protected]         119            2
    [email protected]                                             MGR [email protected]          2            1
    [email protected]                                             MGR [email protected]          2            1
    [email protected]                                              MGR [email protected]         117            2
    9 rows selected.There are multiple matches in REP for the parent group id's. e.g. xyz2@.. and xyz3@.. both have a parent of 2 so you'll get duplicated branches from that as the connect by is going on the group id's e.g.
    1
      2
         117
             112
             112
         119
             115
             115
             115
      2
         117
             112
             112
         119
             115
             115
             115The duplicates are caused by the rows in REP. So Oracle is doing what you are asking of it because there is nothing further to restrict the connection to make one branch of 2 unique from the other branch of 2

  • How to use order by with hierarchical query

    I have a hierarchical query basically it brings back an organization chart. We start with a manager's id, get all that person's employees. If any of the employees is also a manager I want to get that person's employees and return them right after that person. I won't bother with the whole query but relevant part is:
           START WITH em.mgr_id = pi_mgr_id
          CONNECT BY nocycle PRIOR em.emp_id = em.mgr_id;Where pi_mgr_id is a parameter passed to the procedure and em is the alias for the emp_mgr_relationship table which contains emp_id and mgr_id. This works fine. What I want now is for the employees who work for the same manager to appear in name order. The table which contains the employee names is aliased as pe and the name column is called name1. I added the following:
           START WITH em.mgr_id = pi_mgr_id
          CONNECT BY nocycle PRIOR em.emp_id = em.mgr_id
            order by pe.name1;But that put the entire list in name order. What I want is for employees who work for the same manager to be in name order. Let's the manager whose organization I want is named Frank. What I'd like to get is this
    EMP_NAME    MGR_NAME
    Allen       Frank
    Beth        Frank
    Alex        Beth
    Charles     Beth
    Ed          Beth
    Dean        Frank
    George      Frank
    Benny       George
    David       George
    Sam         George
    Dan         Sam
    Harry       Sam
    John        Sam
    Terry       George
    James       Frank
    Ken         Frank
    Mike        Ken
    Warren      KenHow do I get the list in this order?
    Edited by: kendenny on Jul 28, 2010 7:31 AM

    Make use of ORDER SIBLINGS clause in hierarchial queries to set the order by child columns.
    START WITH em.mgr_id = pi_mgr_id
          CONNECT BY nocycle PRIOR em.emp_id = em.mgr_id
            *order siblings by name1;*

  • Problems with Views based on a Hierarchical Query

    Datamodeler 3.1.3.706 (SQL Dev 3.2.10.09):
    When creating a view that utilizes a Hierarchical Query, the Query Builder encounters various difficulties:
    When pasting in SQL code, if the view is saved without first clicking the update diagram button, the object in the view entity relationship diagram provides a faithful representation of the view without errors, but when reopening the view, the code is missing.
    Simple Example using the classic emp table:
    SELECT level lev
          , emp.*
       FROM emp
      CONNECT BY prior empno = mgr
      START WITH mgr        IS NULLIf the update diagram button is pushed to refresh the graphical view. It mangles the connect by clause and the view gets marked with a warning/error icon in the relationship diagram, but the now mangled code remains available on reopening the query builder.
    Same code as above after clicking the Update Diagram button:
    SELECT Level lev
    , emp.*
       FROM emp
      CONNECT BYFurther issues are encountered if the query contains any of the CONNECT_BY_% hierarchical pseudo columns:
    SELECT level
          , emp.*
          , connect_by_root emp.ename root_ename
       FROM emp
      CONNECT BY prior empno = mgr
      START WITH mgr        IS NULL;In this case pasting in the code and clicking either the Update Diagram button or the OK button results in an "Unexpected Token" parsing error.
    These issues are encountered with both the Logical and Relational models.
    Is this a known issue? I've searched this forum but haven't found any references to it.
    Thanks,
    Sentinel

    Hi Sentinel,
    I logged a bug for that.
    You can try DM 3.3 it deals better with first problem, parsing of connect_by_root operator will pass if you don't use alias.
    Philip

  • Joins in Hierarchical Query

    I'm trying to use a Hierarchical Query with table joins in the "FROM" clause. According to the documentation, this will work with Oracle 9i and above.
    My problem is that in some queries, using the "JOIN" syntax works correctly but in others, it gives an "ORA-00928" message.
    If I change the query that gives the error to use old style Oracle joins (using the where clause), the query works.
    Can anyone help me to identify the problem?

    Here's the query. Again, note that if I use the old oracle join syntax (using where clause) it works:
    select level
    , biu.parent_part_no
    , biu.parent_part_serial_no
    , biu.comp_part_qty
    , cpi.part_no
    , cpi.serial_number
    , cpi.trace_code
    from bom.bom_instance_usage biu
    join bom.bom_part_instance cpi
    on cpi.bom_part_instance_key = iu.comp_part_instance_key
    start with ( biu.parent_part_no = '123456'
    and biu.parent_part_serial_no = '114' )
    connect by ( biu.parent_part_no = prior cpi.part_no
    and biu.parent_part_serial_no = prior cpi.serial_number )

Maybe you are looking for

  • DB Cache install error!

    I have OAS9i Enterprise Ed./Win NT SP 6 When I cinfigure Data Base cache I have the follow error: "unable to open RPC connection to external procedure agent" Pls help me

  • How to get the current page URL

    HI All I am working in oracle apps 4.0 I have one page called history in that i have one page item called Application url. My application id is 122 but its a copy of application 106 How to get the current page url for the page item. Any steps should

  • My audio in jack only lets me use apple headphones

    If I want to listen to music through headphones, they have to be apple. Any others won't work. When I try to use an audio in cord to play music through external speakers that won't work either. I've tried this with three sets of apple headphones and

  • Anyone know of a plug-in to create the effect of an old TV switching off?

    I'm editing a film where I would like the screen to switch off (with an audio clunk) like TV's used to do. A bit like this video at 01:26 TV Turn Off and Blip Out Version 3 - YouTube but much more realistic. Thanks!

  • Execute mapping on condition (suggestions plz)

    Hi, I'am loading huge flat files in a warehouse and I need to make sure no file is loaded twice. I guess there are a few possibilities, but feedback is always pleasant. Is it possible to use a pre-mapping operator here? ( ie. user a procedure to chec