Multiple Start with clause-Hiercharical Query

Hi,
Can we use more than 1 "start with" clause .In one of our requirement there can be a parent id can be null or not null
START WITH (gpo.orig_po_confirmation_no  IS NULL   OR gpo.orig_po_confirmation_no  IS NOT NULL)
    CONNECT BY PRIOR gpo.po_confirmation_no =gpo.orig_po_confirmation_no;When I executed I got below error
ORA-01436: CONNECT BY loop in user data
01436. 00000 -  "CONNECT BY loop in user data"
*Cause:   
*Action:Any suggestion to resolve the error
Regards,
Achyut

Achyut K wrote:
Hi,
Can we use more than 1 "start with" clause .In one of our requirement there can be a parent id can be null or not nullNo you cant use more than one START WITH clause. But you can specify multiple CONDITION in the START WITH clause.
When I executed I got below error
ORA-01436: CONNECT BY loop in user data
01436. 00000 -  "CONNECT BY loop in user data"
*Cause:   
*Action:
To diagnose this we need to see the data. One quick try would be to using NOCYCLE in your CONNECT BY clause.

Similar Messages

  • Adding dynamic 'start with' clause in Hierarchial query in Discoverer 4i

    I have created a custom folder based upon the following hierarchial guery:
    SELECT supervisor_name, level
    FROM xxhr084_hr_emps_v
    START WITH supervisor_name = 'TEST SUPER'
    CONNECT BY prior person_id = supervisor_id
    ORDER SIBLINGS by supervisor_name
    I want the 'START WITH' value to be based upon a parameter i.e. the User enters the value of supervisor name at runtime.
    However, I can't use a parameter within the custom folder and I can't generate the 'start with' condition in my workbook.
    Anyone have a solution for populating the start with clause at run-time?
    Thanks,
    Kevin

    Can you create the custom folder without a condition on supervisor_name - i.e. without the supervisor_name = 'X' starts with clause - in the EUL, and then define the condition on the supervisor_name field within the report?
    SELECT supervisor_name, level
    FROM xxhr084_hr_emps_v
    CONNECT BY prior person_id = supervisor_id
    ORDER SIBLINGS by supervisor_name
    You can then restrict the results that the hierarchical query returns (from the custom folder) within the report, by adding a condition on supervisor_name.
    Hope that makes some sense.

  • 'CONNECT BY PRIOR..START WITH'  clause  Usage

    Hi All,
    Could you please let me know the usage of 'connect by prior...start with' clause.
    I only know that it helps for hierarchial retrival,but not aware of details.
    Can someone provide the use with example for the same.
    On searching on the net,I have seen numerous examples but none of them could explain it properly and everywhere the same SCOTT/TIGER schemas EMP and MGR table's example is given which is not enough explanatory.
    Thanks in advance...
    Aashish S.

    suppose u need to get all employees in a company in a hirerchical manner
    ie presdent then mgrs
    then employeess reporting to them
    this can be done using connect by prior and start by
    select empname from emp
    connect by prior empno=mgrno
    start with mgrno is null

  • Avoiding WITH clause when querying Multiple Facts

    OBIEE 10.1.3.4
    We have a report which requires that we query multiple facts, which share the same Item dimension.
    When we run the report, the BI Server generates a query using the WITH clause -
    WITH SAWITH0 ( SELECT.............)
    SAWITH1( SELECT .............)
    SELECT .........
    FROM SAWITH0 FULL OUTER JOIN SAWITH1
    The above query errors out with ORA-918 Column Ambiguously Defined. I cannot find any issue in the query that has been generated.
    The query does fail from SQL Plus with the same ORA error. But each of the select statements executed properly and the complete statement executes when I replace the FULL OUTER JOIN with INNER JOIN.
    I can only assume that this is a bug in Oracle Database.
    Now at times I have seen OBIEE generate separate queries when querying multiple facts and then combine the data together in memory.
    Is there a way that I can force OBIEE to avoid using WITH clause and run separate SELECT statements ? How does the BI Server choose between using a WITH clause or running separate queries ?
    Any help would be appreciated.

    I do not have a multi-dimensional data source.
    My connection pool seems fine to me, I am not sure I need to do anything with the connection pool.
    Hi Turribeach,
    I understand that BI Server uses WITH clause for multiple facts. But I have also seen it issue separate queries against the database.
    So what I am looking to understand under what situation does it issue separate queries i.e. No WITH clause ?
    Thanks

  • Dynamic orderby clause for multiple columns with out Dynamic query

    Hi,
            I've a query like
    "select  * from tablename order by column1,column2,column3,column4,column5,column6"
    in the above query the order by column will be dynamically changed. The query is placed in a stored procedures and the order by column will come by parameter.
       For ex: @orderbycol = column2,column1,column3,column4,column5,column6
                                         or
                    @orderbycol = column3,column2,column1,column4,column5,coumn6
    How can we manage the order by clause as dynamically without go to dynamic query.

    ORDER BY CASE @sortcol1
                 WHEN 'col1' THEN col1
                 WHEN 'col2' THEN col2
             END,
             CASE @sortcol2
                 WHEN 'col1' THEN col1
                 WHEN 'col2' THEN col2
             END,
    Note: these CASE expressions assumes that all columns have the same data type.
    You could consider sorting in the presentation layer instead.
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Multiple sheets with the same query automaticly

    Hi gurus,
    I want to create a workbook with one query and one input variable.
    The input variable is the 0PLANT. And I want to execute the same query so many times as  0PLANPLANTs has the 0PLANT and each 0PLANPLANT appear in different sheets.
    There are different 0PLANTs with 4,5,6,... 0PLANPLANTs and the workbook has to be valid to all 0PLANT and automaticly generate 4,5,6,... sheets depending of the number of 0PLANPLANTs has the 0PLANT.
    Any idea?
    Best regards

    But, what macros? I don´t have idea over macros excel´s.
    Thanks for your answer.

  • How to insert multiple records with a single query?

    Hi,
    I've to save a huge number of installments with their other information such as due on blah blah. Now, I want to add the all of these information at once with a single insert query.
    How can I do that?

    Hi
    What is your source data?
    If the source is external to the SQL Server (like a log file, Excel, CSV, JSON, XML, external application...) you can and should insert it all using Bulk Insert operation.
    Pls clarify what is your source data, and if you need more help using Bulk Insert.
    https://www.simple-talk.com/sql/learn-sql-server/bulk-inserts-via-tsql-in-sql-server/
    http://msdn.microsoft.com/en-us/library/ms188365.aspx
    [Personal Site] [Blog] [Facebook]

  • Hierarchical Query/START WITH/Remote Database error

    Running on: EE 10.1.0.4.0 - 64bit
    Remote DB: EE 11.1.0.6.0 - 64bit Production
    Trying:
    <PRE>
    WITH
    Moo
    AS
    SELECT
    1
    FROM
    Dual@EPO11_Link A
    CONNECT BY
    LEVEL = 1
    START WITH
    EXISTS (SELECT * FROM Dual@EPO11_Link B WHERE A.Dummy = B.Dummy)
    SELECT
    FROM
    Moo;
    </PRE>
    Getting:
    ORA-00904: "A3"."$nso_col_1": invalid identifier
    ORA-02063: preceding line from EPO11_LINK
    00904. 00000 - "%s: invalid identifier"
    *Cause:   
    *Action:
    - The CTE's query runs fine on its own.
    - removing the @ from the first Dual@EPO11_Link , works.
    - removing the @ from the second Dual@EPO11_Link , errors:
    ORA-02016: cannot use a subquery in a START WITH on a remote database
    02016. 00000 - "cannot use a subquery in a START WITH on a remote database"
    *Cause:   
    *Action:
    Error at Line: 10 Column: 24
    - Removing the START WITH clause, works.
    - Changing the START WITH clause's "=" to "IN", works.
    - Changing the START WITH to EXISTS (SELECT * FROM Dual@EPO11_Link B WHERE A.Dummy = B.Dummy) results in:
    ORA-00904: "A3"."DUMMY": invalid identifier
    ORA-02063: preceding line from EPO11_LINK
    00904. 00000 - "%s: invalid identifier"
    *Cause:   
    *Action:
    Am i missing something here?
    Edited by: Brian Tkatch on Jan 19, 2012 1:27 PM

    Searching error messages books:
    These have the ORA-02016:
    8: http://docs.oracle.com/cd/A58617_01/server.804/a58312/newch228.htm#19589
    9: http://docs.oracle.com/cd/A91202_01/901_doc/server.901/a90202/e1500.htm#1001663
    These do not have the ORA-02016:
    10R1: http://docs.oracle.com/cd/B14117_01/server.101/b10744/e1500.htm#ORA-02009
    10R2: http://docs.oracle.com/cd/B19306_01/server.102/b14219/e1500.htm#ORA-02009
    11R1: http://docs.oracle.com/cd/B28359_01/server.111/b28278/e1500.htm#ORA-02009
    Starting with (heh) 10R1 a bunch of error messages were removed regarding hierarchical queries, including this one.
    Is it allowed or not?

  • Connect By query without a Start with

    I have a database that takes a feed from a legacy system that has a rather illogical data structure that I need to tie together. The records chain together where the value in original_col equals the value of another records new_col. Using a connect by prior query I have the following results:-
    Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
    with data_sample as (
            SELECT 'A129994' AS ORIGINAL_COL ,'A129993' AS NEW_COL FROM DUAL
            UNION
            SELECT 'A129996' AS ORIGINAL_COL ,'A129994' AS NEW_COL FROM DUAL
            UNION
            SELECT 'A129999' AS ORIGINAL_COL ,'A129996' AS NEW_COL FROM DUAL
            UNION
            SELECT 'B129994' AS ORIGINAL_COL ,'B129993' AS NEW_COL FROM DUAL
            UNION
            SELECT 'B129996' AS ORIGINAL_COL ,'B129994' AS NEW_COL FROM DUAL
            UNION
            SELECT 'B129999' AS ORIGINAL_COL ,'B129996' AS NEW_COL FROM DUAL       
            UNION
            SELECT 'C129994' AS ORIGINAL_COL ,'C129993' AS NEW_COL FROM DUAL
            UNION
            SELECT 'C129996' AS ORIGINAL_COL ,'C129994' AS NEW_COL FROM DUAL
    select original_col, new_col, level as connect_by_level
    CONNECT BY PRIOR original_col = new_col;
    Results:-
    ORIGINAL_COL            NEW_COL                 CONNECT_BY_LEVEL
    A129994                    A129993                             1
    A129996                    A129994                             2
    A129999                    A129996                             3
    A129996                    A129994                             1
    A129999                    A129996                             2
    A129999                    A129996                             1
    B129994                    B129993                             1
    B129996                    B129994                             2
    B129999                    B129996                             3
    B129996                    B129994                             1
    B129999                    B129996                             2
    B129999                    B129996                             1
    C129994                    C129993                             1
    C129996                    C129994                             2
    C129996                    C129994                             1The result I would like to have is just one chain for each set - however my issue is that I have no logical record for a START WITH clause.
    A129994                    A129993                             1
    A129996                    A129994                             2
    A129999                    A129996                             3
    B129994                    B129993                             1
    B129996                    B129994                             2
    B129999                    B129996                             3
    C129994                    C129993                             1
    C129996                    C129994                             2There is no specific record type that would be a start point such as a null parent, and the ID's used are in no logical order so I can't use the min value or a group b. Any pointers gratefully received.
    Edited by: jweb on 12-Jul-2012 04:51

    I think you need found root for your tree
    check following example
    with data_sample as
    (select 'A129994' as ORIGINAL_COL, 'A129993' as NEW_COL
        from DUAL
      union
      select 'A129996' as ORIGINAL_COL, 'A129994' as NEW_COL
        from DUAL
      union
      select 'A129999' as ORIGINAL_COL, 'A129996' as NEW_COL
        from DUAL
      union
      select 'B129994' as ORIGINAL_COL, 'B129993' as NEW_COL
        from DUAL
      union
      select 'B129996' as ORIGINAL_COL, 'B129994' as NEW_COL
        from DUAL
      union
      select 'B129999' as ORIGINAL_COL, 'B129996' as NEW_COL
        from DUAL
      union
      select 'C129994' as ORIGINAL_COL, 'C129993' as NEW_COL
        from DUAL
      union
      select 'C129996' as ORIGINAL_COL, 'C129994' as NEW_COL from DUAL)
    select original_col, new_col, level as connect_by_level
      from (select ds.*,
                   case (select count(*)
                       from data_sample dsc
                      where dsc.original_col = ds.new_col
                        and rownum < 2)
                       when 0 then
                        1
                       else
                        0
                   end as is_root -- try find parent in table if parent not exists this record is root
              from data_sample ds) ds
    start with is_root = 1
    connect by prior original_col = new_col;good luck

  • Multiple with clause

    Hello guys.
    Can i use multiple time WITH clause in one query..
    I can't find the way.
    I am trying to do somtehing like :
    with a as
    select 1 from x
    with b as
    select 2 from y
    select a.1,b.2
    from a , b

    HI all,
    I have a requirement to write insert script using with clauses, some thing like this...
    with a as
      select 1 as col from x
    b as
      select 1 as col1, 2 as col2 from y
       where 1 = a.col
    insert into y
    (id,1, 2)
    select seq.nextval,
           a.col,
           b.col2
    from   a, b
    Please advice.

  • 'Connect By' and 'Start With' in Discoverer

    Hello,
    I have a hierarchal query to show all child organizations under the parameter ':Parent_ID'
    This is the query, I execute it in SQL Developer normally:
    SELECT el.organization_id_child, ch.name,level*
    FROM hr.hr_all_organization_units org,*
    hr.PER_ORG_STRUCTURE_ELEMENTS el      ,*
    hr.hr_all_organization_units ch*
    WHERE el.org_structure_version_id         = 61*
    AND org.organization_id                     = el.organization_id_parent*
    AND ch.organization_id                      = el.organization_id_child*
    START WITH el.organization_id_parent = :Parent_ID*
    CONNECT BY el.organization_id_parent = PRIOR el.organization_id_child*
    in the below line:
    START WITH el.organization_id_parent = :Parent_ID*
    The hierarchal query will start with parameter value to show all childes under that organization...
    How can I use this simple query in Discoverer !! I am trying to find the START WITH in the discoverer also can not put this query in a new custom folder without removing the parameters.
    Please Help...
    Regards
    Edited by: adelshehri on Oct 21, 2009 2:47 PM

    Hi,
    If you need to use the hierarchical query then you will not be able to pass a Discoverer parameter directly to the start with clause in the custom folder.
    However, you can use a database context as a session parameter and use this parameter in the customer folder. More details on this can be found at LearnDiscoverer
    Also you could put your hierarchical query into a table function passing the parent_id as a parameter to the table function. Then in your custom view you would join this to a table that returns the list of parent ids that you wish to use. e.g.
    select f.organization_id_child, f.name,level
    from hr.hr_all_organization_units par
    , table (hierarchy_function(par.parent_id) f
    where par.organization_id = :parent_id
    Rod West

  • Hierarchical connect by and start with and joins?

    I've got an employees table and an identifiers table. The identifiers table is hierarchical, with parents and children. Each employee has one or more identifiers, but only one identifier is considered the "primary" or root identifier for each employee. Unfortunately, the employee table might be pointing at one of the child identifier rows and not the root. I need a fast query to join the employees with their most current (root) identifier.
    Here's code to define the problem.
    create table employees (employeeid varchar2(8), fakeNationalID varchar2(9), empname varchar2(30));
    insert into employees (employeeid, fakeNationalID, empname) values (1,'001000001','John Smith');
    insert into employees (employeeid, fakeNationalID, empname) values (2,'002000002','James Jones');
    create table realids (realidkey NUMBER, fakeNationalID VARCHAR2(9) not null,
       realNationalID VARCHAR2(9) UNIQUE, parent_realidkey number);
    insert into realids (realidkey, fakeNationalID, realNationalID, parent_realidkey) values
       (1,'001000001','111111111',3);
    insert into realids (realidkey, fakeNationalID, realNationalID, parent_realidkey) values
       (2,'002000002','222222222',null);
    insert into realids (realidkey, fakeNationalID, realNationalID, parent_realidkey) values
       (3,'003000003','333333333',null);
    commit;  
    create or replace function get_parentid (fakeID in VARCHAR2) return varchar2 is
       tempid VARCHAR2(9);
       begin
          select realNationalID into tempid
             from (
               select realNationalID, fakeNationalID
                  from realids
                  start with fakeNationalID = fakeID
                  connect by nocycle prior parent_realidkey = realidkey
                  order by level desc)
                  where rownum = 1;
          return tempid;
          exception
             when NO_DATA_FOUND then
                return NULL;
             when others then raise;
        end;
    select get_parentid('001000001') from dual; -- returns 333333333 because its linked to a parent
    select get_parentid('002000002') from dual; -- returns 222222222 because there is only one child
    select get_parentid('003000003') from dual; -- returns 333333333 because it is the parentWhat I want is to get the highest parent node in realids for each row in employees...
    This works, but is NOT very efficient:
    select employeeid, get_parentid(fakeNationalID) realid, empname from employees;
    employeeid   realid       empname
    1            333333333     John Smith
    2            222222222     James JonesYou can imagine what this would be like with 100K rows or greater. It takes about 3 minutes to run.
    This seemed like a good way to do this, but with a sub query.
    select e.employeeid, e.fakenationalid, e.empname, sub.realnationalid
       from employees,
          (select realidkey, fakenationalid, realnationalid, parent_realidkey
             from realids r
             start with r.fakenationalid = e.fakenationalid
             connect by prior r.parent_realidkey = r.realidkey) subUnfortunately, it produces an invalid identifier on e.fakenationalid (in the start with clause).
    Anyone have any ideas on how to get the top most parent node from the realids for each row in the employees table? In real life there are 6 or more employees tables spread across several remote instances some of which point at children in the realids table and some of which point at the parents. We always want the top most parent realid. Any help would be much appreciated.

    Hi,
    Thanks for posting the sample data in such a convenient form!
    It always helps to post your Oracle version, too, especially when dealing with CONNECT BY queries.
    The following does what you requested in Oracle 10:
    WITH     got_roots   AS
         SELECT     CONNECT_BY_ROOT     fakenationalid     AS leaf_id
         ,     realnationalid
         FROM     realids
         WHERE     CONNECT_BY_ISLEAF     = 1
         START WITH      fakenationalid IN ( SELECT  fakenationalid
                                              FROM    employees
         CONNECT BY     realidKEY     = PRIOR parent_realidkey
    SELECT     e.employeeid
    ,     r.realnationalid
    ,     e.empname
    FROM     employees     e
    JOIN     got_roots     r     ON     r.leaf_id     = e.fakenationalid
    ;In any query, calling a user-defined function for each row is going to be slow. Fortunately, Oracle now has built-in functions and operators that can take the place of get_parentid. The CONNECT_BY_ROOT operator, which was introduced in Oracle 10, is the key to this problem. In Oracle 9, you can get the same results using SYS_CONNECT_BY_PATH.
    It's usually faster to do the CONNECT BY query separately, and then join whatever other tables you need to the results.
    You had a good idea in your last query. The problem was that sub and employees were equal tables in the FROM clause, and you can't correlate equals. You can only correlate a sub-query to its super-query. You could make that general idea work by changing sub into a scalar sub-query,which could be correlated to employees, but I think it would be a lot less efficient than what I posted above.

  • START WITH and CONNECT BY in Oracle SQL ( hierarchical relationship)

    Hi, the original table as below
    Customer_ID         Account_ID          Paying_Account_ID         Parent_Account_ID          Company_ID
    158                    158                    158                         158                     0
    159                    159                    158                         158                     0
    160                    160                    158                         158                     0
    181                    181                    181                         181                     0
    183                    183                    183                         183                     0
    24669                  24669                  24669                       24669                   0        
    24671                  24671                  24671                       24669                   0
    24670                  24670                  24670                       24669                   0    
    3385127                3385127                3385127                     24670                   0To identify the hierarchical relationship of the data, which are PARENT_ACCOUNT_ID & ACCOUNT_ID, below is the query that I was used.
    select  lpad(' ', 2*level) || A.ACCOUNT_ID AS LEVEL_LABEL, CONNECT_BY_ISCYCLE "Cycle", LEVEL, A.* from ACCOUNT A
    START WITH parent_account_id = account_id
    CONNECT BY NOCYCLE  PRIOR A.ACCOUNT_ID = A.PARENT_ACCOUNT_ID
    AND account_id != parent_account_id
    ;This is the result from the query
    Level_Label              Level          Cycle        Customer_ID             Account_ID        Paying_Account_ID      Parent_Account_ID      Company_ID
    158                         1             0              158                     158              158                   158                     0
       159                      2             0              159                     159              158                   158                     0
       160                      2             0              160                     160              158                   158                     0
    181                         1             0              181                     181              181                   181                     0
    183                         1             0              183                     183              183                   183                     0
    24669                       1             0              24669                   24669            24669                 24669                   0      
        24671                   2             0              24671                   24671            24671                 24669                   0
        24670                   2             0              24670                   24670            24670                 24669                   0
            3385127             3             0              3385127                 3385127          3385127               24670                   0My questions is how can I modified the query in order to calcuate the values for:
    My_Total_PR - Number of my child PR accounts which doest not include itself.
    Total_PR - Total number of PR accounts in the overall structure
    My_Total_NPR - Number of my child NPR accounts which doest not include itself.
    Total_NPR - Total number of NPR accounts in the overall structure
    *PR stand for payment responsible, for instance the payment responsible for Account 158 is 158 (Paying_Account_ID), so the Total_PR for 158 is 3 (158, 159, 160)
    *NPR stand for Non payment responsible, for instance the payment responsible for Account 159 is 158 (Paying_Account_ID), so the Total_NPR for 159 is 1
    This is the expected result, Any advice much appreciated. Thanks
    Level_Label                     Level           Cycle           My_Total_PR     Total_PR     My_Total_NPR     Total_NPR     Paying_Account
    158                               1                0                  2              3          0              0              158
        159                           2                0                  0              0          0              1              158
        160                           2                0                  0              0          0              1              158
    181                               1                0                  0              1          0              0              181
    183                               1                0                  0              1          0              0              183
    24669                             1                0                  0              1          3              3              24669                  
        24671                         2                0                  0              1          0              0              24671
        24670                         2                0                  0              1          1              1              24670
            3385127                   3                0                  0              1          0              0              3385127Edited by: user11432758 on 14-Feb-2012 01:00
    Edited by: user11432758 on 14-Feb-2012 07:05

    Hi,
    user11432758 wrote:
    Hi below is the DDL statment, thanks
    CREATE TABLE "SYSTEM"."ACCOUNT" ...
    Don't create your own objects in the SYSTEM schema, or any schema that comes with the database. Create a separate schema, and put your objects into it. You'll have fewer security problems, and migrating to a new database will be easier.
    Here's one way to can get the aggregates that you want:
    WITH     got_descendants          AS
         SELECT     CONNECT_BY_ROOT account_id     AS ancestor_id
         ,     paying_account_id
         ,     LEVEL                    AS lvl
         FROM     account
         CONNECT BY NOCYCLE     PRIOR account_id     = parent_account_id
              AND          account_id          != parent_account_id
    SELECT       ancestor_id
    ,       COUNT (CASE WHEN lvl             > 1
                      AND  ancestor_id  = paying_account_id THEN 1 END)     AS my_total_pr
    ,       COUNT (CASE WHEN ancestor_id  = paying_account_id THEN 1 END)     AS total_pr
    ,       COUNT (CASE WHEN lvl             > 1
                      AND  ancestor_id != paying_account_id THEN 1 END)     AS my_total_npr
    ,       COUNT (CASE WHEN ancestor_id != paying_account_id THEN 1 END)     AS total_npr
    FROM       got_descendants
    GROUP BY  ancestor_id
    ;Output:
    `             MY_         MY_
                TOTAL TOTAL TOTAL TOTAL
    ANCESTOR_ID   _PR   _PR  _NPR  _NPR
            158     2     3     0     0
            159     0     0     0     1
            160     0     0     0     1
            181     0     1     0     0
            183     0     1     0     0
          24669     0     1     3     3
          24670     0     1     1     1
          24671     0     1     0     0
        3385217     0     1     0     0This gives you the right numbers, but how can we get them in an order that reflects the hierarchy, with the columns (such as lvl) that are derived from the hierarchy?
    One way would be to do two CONNECT BY queries; one without a START WITH clause (like the one above) that gets the aggregates, and the other with a START WITH clause (like your original query), that is in the right order, and has columns such as level_label and level. We could join the result sets and get exactly what we want. I'll leave that as an exercise.
    Here's another way, that gets the right results with only one CONNECT BY query:
    WITH     got_descendants          AS
         SELECT     CONNECT_BY_ROOT account_id     AS ancestor_id
         ,     paying_account_id
         ,     account_id
         ,     LEVEL                    AS lvl
         ,     CONNECT_BY_ISCYCLE          AS cycle
         ,     CASE
                  WHEN  CONNECT_BY_ROOT account_id
                      = CONNECT_BY_ROOT parent_account_id
                  THEN  ROWNUM
              END                    AS r_num
         FROM     account
         CONNECT BY NOCYCLE     PRIOR account_id     = parent_account_id
              AND          account_id          != parent_account_id
         ORDER SIBLINGS BY     account_id     -- Optional
    ,     got_o_num     AS
         SELECT     got_descendants.*
         ,     MIN (r_num) OVER (PARTITION BY  account_id)     AS o_num
         ,     MAX (lvl)   OVER (PARTITION BY  account_id)      AS max_lvl
         FROM     got_descendants
    SELECT       LPAD ( ' '
                , 2 * (MIN (max_lvl) - 1)
                )  || ancestor_id                         AS level_label
    ,       MIN (max_lvl)                                AS "Level"
    ,       MIN (cycle)                                   AS "Cycle"
    ,       COUNT (CASE WHEN lvl             > 1
                      AND  ancestor_id  = paying_account_id THEN 1 END)     AS my_total_pr
    ,       COUNT (CASE WHEN ancestor_id  = paying_account_id THEN 1 END)     AS total_pr
    ,       COUNT (CASE WHEN lvl             > 1
                      AND  ancestor_id != paying_account_id THEN 1 END)     AS my_total_npr
    ,       COUNT (CASE WHEN ancestor_id != paying_account_id THEN 1 END)     AS total_npr
    ,       MIN (paying_account_id)                                    AS paying_account
    FROM       got_o_num
    GROUP BY  ancestor_id
    ORDER BY  MIN (o_num)
    ;Output:
    `                             MY_         MY_
                                TOTAL TOTAL TOTAL TOTAL  PAYING_
    LEVEL_LABEL     Level Cycle   _PR   _PR  _NPR  _NPR  ACCOUNT
    158                 1     0     2     3     0     0      158
      159               2     0     0     0     0     1      158
      160               2     0     0     0     0     1      158
    181                 1     0     0     1     0     0      181
    183                 1     0     0     1     0     0      183
    24669               1     0     0     1     3     3    24669
      24670             2     0     0     1     1     1    24670
        3385217         3     0     0     1     0     0  3385217
      24671             2     0     0     1     0     0    24671This is exactly what you requested, except that you posted the row with level_label='  24671' before the row with level_label='  24671'. You may not care which of those comes first, but if that's important, explain why those rows need to be in descending order by account_id, while '159 and '160' are in ascending order. You'll need to change the ORDER SIBLINGS BY clause accordingly.

  • 'Missing select' error for update statement using WITH clause

    Hi,
    I am getting the below error for update statement using WITH clause
    SQL Error: ORA-00928: missing SELECT keyword
      UPDATE A
      set A.col1 = 'val1'
         where
      A.col2 IN (
      WITH D AS
      SELECT col2 FROM
      (SELECT col2, MIN(datecol) col3 FROM DS
      WHERE <conditions>
        GROUP BY PATIENT) D2
      WHERE
      <conditions on A.col4 and D2.col3>

    Hi,
    The format of a query using WITH is:
    WITH  d  AS
        SELECT  ...  -- sub_query
    SELECT  ...   -- main query
    You don't have a main query.  The keyword FROM has to come immediately after the right ')' that ends the last WITH clause sub-query.
    That explains the problem based on what you posted.  I can't tell if the real problem is in the conditions that you didn't post.
    I hope this answers your question.
    If not, post a complete test script that people can run to re-create the problem and test their ideas.  Include a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data.
    In the case of a DML operation (such as UPDATE) the sample data should show what the tables are like before the DML, and the results will be the contents of the changed table(s) after the DML.
    Explain, using specific examples, how you get those results from that data.
    Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • Create sequence start with variable

    Trying to create a sequence within plsql passing a variable into the start with clause. I am getting the 'ora-01722: invalid number' error. Thoughts?
    -- Created on 06-Jun-07 by CARSONBC
    declare
    -- Local variables here
    v_staff_proxy_user_seq_num number;
    begin
    -- find max value
    select max(staff_proxy_user_id)+1
    into v_staff_proxy_user_seq_num
    from icmr32dv.staff_proxy_user;
    -- create sequence
    execute immediate 'create sequence icmr32dv.staff_proxy_user_seq2 minvalue 1 maxvalue 999999999999999999999999999 start with v_staff_proxy_user_seq_num increment by 1 nocache';
    end;

    What is the requirement of creating sequnce dynamically.?
    may be you can try this?
    execute immediate 'create sequence icmr32dv.staff_proxy_user_seq2 minvalue 1 maxvalue 999999999999999999 start With  '|| v_staff_proxy_user_seq_num || '  increment by 1 nocache';

Maybe you are looking for

  • Is it possible to change a new one if the Iphone 5 wake button is not working?

      I heard the iphone 5 wake up button has a disign defect. Will they give me a free repairing even my iphone is over warranty?

  • Can Thunderbird be opened to a folder other than inbox.

    We're all pretty much flooded with junk mail. I'm to the point where it would be more effective to open Mozilla to a custom folder (i.e. not inbox) and populate that folder with selected contacts (i.e. not all contacts). Any thoughts? filtering out j

  • Excel 2013 registry entry for removing the protected view in excel.

    Hi All I am having Office 2013 on my machine. I wanted to remove protected view from my excell which is showing up when I convert pdf to excell using total pdf converter. The protected view is in red color mode. When I tried searching for the same on

  • How to remove update

    I keep getting the notice that there is a printer update ready from the app store. I don't want this update. How can I put this update alert on hold without stopping other update alerts that I may want to install?

  • Installation of OS X on Windows PC

    This may be a stupid question to many but I'm not too savvy on the whole hardware/software relationship issue. Anyway, if someone could explain it to me in simple to intermediate technical terms to help my finite brain comprehend, I'd appreciate it.