Understanding "CONNECT BY" and "START WITH".

OK, I'm trying to update the rows with values determined by lines joined in a recursive relationship of unknown depth. I am told that "CONNECT BY" and "START WITH" can be useful in this, but I don't see how to get the value I'm looking for.

In my case, there are 3 values in my table.
ID
ID of the parent
Invoice

On some lines, the Bill is null. For records, you get the ID of the invoice by searching for the invoice of the parent folder. I'm trying to update the table so that all THE rows in the table have an ID of invoice.

Here is an example of table and the lines.
CREATE TABLE DISTRIBUTION (
ID            INT,
INV_NUM       INT,
PARENT_ID     INT
)

INSERT INTO DISTRIBUTION 1, 111, NULL;
INSERT INTO DISTRIBUTION 2, 112, NULL;
INSERT INTO DISTRIBUTION 3, NULL, 2;
INSERT INTO DISTRIBUTION 4, 113, NULL;
INSERT INTO DISTRIBUTION 5, NULL, 4;
INSERT INTO DISTRIBUTION 6, NULL, 5;
INSERT INTO DISTRIBUTION 7, NULL, 6;
What I would do is update the inv_num column in the table so that a select statement * would look like this...
ID        INV_NUM    PARENT_ID
-----     -------------     ---------------
1            111           null
2            112           null
3            112              2
4            113           null
5            113              4
6            113              5
7            113              6
You can provide any help would be greatly appreciated.

Hello

Thank you post the CREATE TABLE and INSERT instructions, but please make sure that they work.
None of the INSERT statements; I think you meant something like the statements shown after the query.

Here's a way to get the desired results:

UPDATE  distribution     m
SET     inv_num = ( SELECT  inv_num
              FROM    distribution
              WHERE   CONNECT_BY_ISLEAF     = 1
              START WITH     id          = m.id
              CONNECT BY     id          = PRIOR parent_id
                       AND     PRIOR inv_num     IS NULL
               )
WHERE   inv_num       IS NULL
;

This statement is Bottom-Up of subqueries, where we START WITH the lines that need to update, and process to the top of the tree, until you get to an ancestor who was an inv_num.
In your sample data, only the roots (the lines that have no parents) have inv_num. In this case, it might be a little easier (but only a little) to make a request from top to bottom , where we START WITH the roots and low process in the tree to find their subordinates.
If we add some data examples where a nonroot has inv_num:

INSERT INTO DISTRIBUTION (id, inv_num, parent_id) VALUES ( 91, 910,   1);
INSERT INTO DISTRIBUTION (id, inv_num, parent_id) VALUES ( 92, NULL, 91);

What results would you like?
Using the UPDATE statement above, id = 92 would get his inv_nuym of the closest ancestor (in the case of thios, parent) who had an inv_num:

.       ID    INV_NUM  PARENT_ID
---------- ---------- ----------
         1        111
        91        910          1
        92        910         91
         2        112
         3        112          2
         4        113
         5        113          4
         6        113          5
         7        113          6

Either the row with id = 92 gets inv_num = 910, no 111.

Tags: Database

Similar Questions

  • Hierarchical connect by and start with and joined?

    I have an Employees table and a table of identifiers. The table of identifiers is hierarchical, with parents and children. Each employee has one or more identifiers, but that a unique identifier is considered to be the "primary" or root for each employee identifier. Unfortunately, the employee table can point to one of the children identifier lines and not the root. I need a quick query to reach employees with their most recent ID (root).

    Here's the 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 parent
    What I want is to put the parent node above realids for each line of employees...

    It works, but it is NOT very effective:
    select employeeid, get_parentid(fakeNationalID) realid, empname from employees;
    employeeid   realid       empname
    ----------   -----------  ------------
    1            333333333     John Smith
    2            222222222     James Jones
    You can imagine what it would be like with 100K lines or more. It takes about 3 minutes to run.

    It seemed like a good way to do it, 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) sub
    Unfortunately, it produces an invalid identifier on e.fakenationalid (in the beginning with the clause).

    Anyone has any ideas on how to get top most parent node of the realids for each row in the employees table? In real life, there are 6 or more employees tables across multiple remote instances of what children in the realids table and how much to the parents. We always want the highest parent of the page realid. Any help would be appreciated.

    Hello

    Thanks for posting the sample data in a convenient form!
    It is always useful to post your version of Oracle, too, especially when it comes with CONNECT BY queries.

    What follows is what you asked for 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, call a function defined by the user for each line is going to be slow. Fortunately, Oracle now has the 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 the problem. In Oracle 9, you can get the same results using SYS_CONNECT_BY_PATH.

    It is generally faster to CONNECT BY query separately, and then join some other tables you need for results.

    You had a good idea in your last query. The problem was that void and employees were equal tables in the FROM clause, and you cannot establish a correlation between equals. You can only correlate a subquery to its Super application. You could make to this general idea work by changing void in a scalar sub-requete, which can be connected to the employees, but I think it would be much less effective than what I posted above.

  • Join the two trees connect by prior Start With and return only common records?

    Oracle 10g Release 2 (10.2)

    I have two tables which have structured data. The results, when running queries individually are correct, but I need to join tree a tree two to get only the common records between them.

    -Trees a
    SELECT ip_entity_name, entity_code, hier_level, entity_parent
    Of ip_hierarchy
    WHERE hier_level > = 3
    CONNECT BY PRIOR Entity_code = entity_parent
    START WITH entity_code = "MEWWD";

    -The two tree
    SELECT ip_entity_name, entity_code, hier_level, entity_parent
    Of ipt_hierarchy
    WHERE hier_level > = 3
    CONNECT BY PRIOR Entity_code = entity_parent
    START WITH entity_code = "IPNAM";


    If I understand correctly, the joints can not work with CONNECT BY / START WITH queries?

    A WITH clause is an option?

    If possible, I don't want to put a selection in a database to display object and join against other queries.

    Thank you.

    Hello

    jtp51 wrote:
    Oracle 10g Release 2 (10.2)
    ...
    If I understand correctly, the joints can not work with CONNECT BY / START WITH queries?

    Before Oracle 9 it was true. Since you're using Oracle 10, you can if you wish; but I'm guessing that you don't want in this case.

    A WITH clause is an option?

    If possible, I don't want to put a selection in a database to display object and join against other queries.

    Yes, a WITH clause that is an option. Viewed online, as Zhxiang has shown, are another option. Either way gives you a regular display effect without creating a database object.

    You did not show a sample and the results, so no one can tell if a join is really what you want. Other possibilities include INTERSECT or an IN subquery.

  • Restarting Lightroom - how * and * recorded preferences (and start with the specified catalog)

    I know how to restart Lightroom (with specified catalogue), but the preferences are not saved:

    Win: lightroom.exe - restart {catalog}

    Mac: Open it {catalog of}

    But sometimes (often) it would be preferable if the preferences were recorded in endangered.

    PS - I can out Lightroom preferences recorded by the farce of the keyboard (Alt/Cmd-FX), but how to get it to start (with specified catalog).

    Any ideas?

    Rob

    Thanks John - big time!

    I got it with bat:

    Taskkill /im lightroom.exe/t

    Lightroom.exe catalog

    Notes:

    * / t was necessary

    * - reboot must be omitted.

    Bat file text:

    taskkill /im lightroom.exe /t
    "C:\Program Files\Adobe\Adobe Photoshop Lightroom 5.2\lightroom.exe" %1
    

    A big thanks again to John - you are my hero.

    Rob

  • Remote Access Auto Connection Manager and error with a VPN work

    I use my laptop to connect to my VPN working. It has not worked since June 24, 2010. I get a message indicating that the connection to network access device is not found. I also have a problem with the connection manager automatic remote access. I'm trying to launch and get an error code 5, unauthorized. The Auto Connection Manager remote access has something to do with the vpn access problem and if so how can I solve this problem?

    Hello hitherandthee,

    Your question of Windows Vista is more complex than what is generally answered in the Microsoft Answers forums. It is better suited for the platform of networking on TechNet. Following your question thanks for posting the link below:

    http://social.technet.Microsoft.com/forums/en-us/winserverPN/threads?page=10

    Thank you
    Irfan H, Engineer Support Microsoft Answers. Visit our Microsoft answers feedback Forum and let us know what you think.

  • I wan to use connections cable and wireless with Windows 7.

    I use Windows 7. I have a wired ethernet connection, but also internal wireless.

    I want to be able to use both connections. I'm using VMware Workstation and it allows me to specify which adapter to use with the virtual machines. However, when Windows 7 detects there are two cables and wireless, it disables the wireless and automatically uses cable.

    How to use both?

    Hi Michael,

    Thanks for the reply. By the brand and model of the PC, I see that there is not a driver designed for Windows 7. Try installing the driver in compatibility mode. Is your problem with the virtual machines that you have running, the host computer, or both? If it's on the host computer and that you have installed the latest driver in compatibility mode for Windows 7, then it is probably a compatibility problem of driver/hardware that by default, this feature in Windows 7 works.

    I hope this helps! Shawn - Support Engineer - MCP, MCDST
    Microsoft Answers Support Engineer
    Visit our Microsoft answers feedback Forum and let us know what you think

  • media, stops and starts with the cursor movement on the screen

    When you watch a video on Youtube or any media file on the internet, it plays very well, then stop so he could continue to play I move the cursor on the video screen, as soon as I stop moving the cursor, it will continue to play and then stop again.
    In other words if I want to display a media file I need to keep the mouse move the cursor over the corner of the screen of media.

    I have no problems with playback of multimedia files stored on my computer. My operating system is Windows 7

    I hope you can help me

    Hello Craig Barnett.

    Thank you to contact the community Microsoft and we will be happy to help you with your concern.

    According to the description, it seems that you are having problems with online videos don't play do not.

    Perform the steps from the link if you have Internet Explorer and check.

    The video problems when you use Internet Explorer

    Answer to us if you are experiencing problems with Internet Explorer or any Windows problem, and I'd be happy to help you.

    Good day!

    Hope this information helps.

  • This query gives the original sequence that increment of original value and start with original number?

    Thank you!

    Select dbms_metadata.get_ddl ('SEQUENCE', 'COURSE_NO_SEQ') of double;

    Hello

    No it will not give the sequence. This u give query will be, how U created the sequence, I mean how u written code for the design of this sequence. That's all...

    After you generate the query, you can use it in another schema, if the sequence is not available with this same name. After you have compiled successfully in other patterns, this sequence will start to give the starting number sequence number...

    See you soon!

  • I have installed 7.0.1. Everytime I open something like Gmail or Google Plus, it indicates the version of Firefox is obsolete and asked to upgrade to the new version. I uninstalled everything and started with a fresh install and still the same problem.

    Even this site think I have Firefox 3.2.

    I have no filter = general.useragent.extra.firefox

    nothing appears when I type that.

    Using Firefox 8.0, mac os x 10.7.2

  • START WITH and CONNECT BY PRIOR

    Hello

    Database: Oracle 11g

    1.

    SELECT ename, empno, mgr

    WCP

    START WITH empno = 7839

    CONNECT BY PRIOR MGR = EMPNO

    Result set:

    EMPNO, ENAME MGR

    -------- ---------- ----------

    KING 7839

    2.

    SELECT empno.ename, Bishop

    WCP

    START WITH mgr = 7839

    CONNECT BY PRIOR MGR = EMPNO

    Result set:

    EMPNO, ENAME MGR

    -------- ---------- ----------

    7566 JONES 7839

    7698 BLAKE 7839

    7782 CLARK 7839

    KING 7839

    KING 7839

    KING 7839

    My questions are:

    Q1. What is actually happening in the result defines two queries. I'm not able to grasp the difference when I use START WITH empno = 7839 and START WITH mgr =. 7839

    Q2. What is the difference between

    CONNECTION BY MGR PRIOR = EMPNO and

    CONNECT BY PRIOR EMPNO = MGR?

    can someone please help me here?

    Thank you

    Hello

    A CONNECT BY query looks like an operation UNION ALL of the data of different levels, numbered 1, 2, 3 and more.

    Level 1 is filled with the START WITH clause.

    If there are data on level N, then N + 1 level is filled, using the CONNECT BY clause, which generally refers to something on the N level via the PRIOR operator.  Another way to put it is that level N + 1 is filled by a self-join with lines that have already chosen the level N.

    If there is no data on the level of N, the query stops.

    Let's see how this applies to your queries.

    Level being such an important concept in CONNECT BY queries, you might want to see in all your CONNECT BY queries all test and debug the.

    1 query, including the level are:

    SELECT ename, empno, mgr

    LEVEL

    FROM scott.emp

    START WITH empno = 7839

    Empno = mgr PRIOR CONNECTION

    ;

    You will notice that I have re-arranged the CONNECT BY clause.  I find it a little more clear medium.  Of course, it never changes the results just if you say "x = y" or "y = x.

    The results, including the level, are:

    LEVEL OF ARCHBISHOP EMPNO, ENAME

    ---------- ---------- ---------- ----------

    7839 KING 1

    What happened to produce these results?

    First level 1 has been met using the START WITH clause.  Level 1 is identical to the results of

    SELECT ename, empno, mgr

    AS LEVEL 1

    FROM scott.emp

    WHERE empno = 7839 - same as your START WITH clause

    ;

    It happens to be only 1 row in the table scott.emp who met the empno = 7839 condition, and we show a few columns of this line.

    That's all that need the level 1.  Something has been on level 1, so we're trying now to complete level 2, using the CONNECT BY condition.

    Any line that is included in the level 2 meets the empno = mgr PREREQUISITE condition, where the PREVIOUS operator refers to a line of level 1.  In this case, there is only 1 row at level 1, this line gets to have a NULL mgr.  Given that PRIOR mgr is NULL in this case, the condition to connect BY

    EmpNo = mgr BEFORE equals

    EmpNo = NULL and who obviously won't be true for any line, so nothing is added to level 2, and ends the query.

    Now let's look at application 2.  I'll add another column of debugging, called path, which I'll describe later:

    SELECT ename, empno, mgr

    LEVEL

    , SYS_CONNECT_BY_PATH (empno, "/") AS path

    FROM scott.emp

    START WITH mgr = 7839

    CONNECT BY PRIOR Mgr = empno

    LEVEL CONTROL

    path

    ;

    Output:

    EMPNO, ENAME MGR LEVEL PATH

    ---------- ---------- ---------- ---------- ---------------

    7566 7839 1 7566 JONES

    7698 7839 1 7698 BLAKE

    7782 7839 1 7782 CLARK

    7839 KING 2/7566/7839

    7839 KING 2/7698/7839

    7839 KING 2/7782/7839

    Again, we'll study how people got 1 level.  It happens to be 3 scott.emp lines that meet this condition START WITH, so there are 3 lines in the game at level 1.

    Given that the data on the level 1, the test of the query to complete level 2, referring to some PRIOR line on level 1.  Any line that meets the condition to connect BY, with a line any level 1 in the PREVIOUS line, will appear at level 2.

    Let's look at the line at level 1 where ename = 'JONES '.  Are there lines in sccott.emp that met the empno = mgr PREREQUISITE condition, where mgr PREREQUISITE is the column of Archbishop of the line with "JONES"?  Yes, there are one, the line with ename = 'KING', so that the rank is included at level 2.

    Let's look at the line at level 1 where ename = 'BLAKE '.  Are there lines in sccott.emp that met the empno = mgr PREREQUISITE condition, where mgr PREREQUISITE is the column of Archbishop of the line with "BLAKE"?  Yes, there are one, the line with ename = 'KING', so that the rank is included at level 2.

    Let's look at the line at level 1 where ename = 'CLARK '.  Are there lines in sccott.emp that met the empno = mgr PREREQUISITE condition, where mgr PREREQUISITE is the column of Archbishop of the line with 'CLARK '?  Yes, there are one, the line with ename = 'KING', so that the rank is included at level 2.

    There are thus 3 rows at level 2.  They happen to all be on the same line of the table emp; It is correct.  Remember, CONNECT BY is like a UNION ALL (not just a UNION).  It is a UNION of

    lines that are at level 1, because him meets the condition to BEGIN WITH, and

    lines that are at level 2 because puts it CONNECT BY condition regarding the 'JONES', and

    lines that are at level 2, because they meet the condition to connect BY regarding the "BLAKE", and

    lines that are at level 2, because they meet the condition to connect BY regarding the "CLARK".

    SYS_CONNECT_BY_PATH can enlighten us on that.  SYS_CONNECT_BY_PATH (empno, ' / ') shows the empno of each level that caused this line appears in the result set.  It's a delimited list /, where the nth element (i.e. the empno after bar oblique nth) is the empno who found the N level.

    Since there were data at level 2, the quert now trying to complete level 3.

    Is there all the rows in the table that satisfy the CONNECT BY condition (mgr PRIOR = empno) with respect to any line level 2?  No, Bishop is be NULL on all lines of level 2, so no line can satisfy this condition CONNECT BY, no lines are added at level 3, and ends the query.

    I hope that answers the question:

    Q1. What is actually happening in the result defines two queries. I'm not able to grasp the difference when I use START WITH empno = 7839 and START WITH mgr =. 7839

    I'll try to not be so detailed answering

    Q2. What is the difference between

    CONNECTION BY MGR PRIOR = EMPNO and

    CONNECT BY PRIOR EMPNO = MGR?

    These 2 CONNECT BY conditions are different where you put the PRIOR operator.  The operator PRIOR to switching as it change the direction, upward or downward, which move you through the tree you get from level to level.

    Bishop PRÉALABLE = empno means the employee on level N + 1 will be the same as the Manager of level N.  This means that higher level numbers will be the most senior people in the hierarchy.  This is called a query from the bottom up.  (Both of the queries that you have posted this same CONNECT BY exact state; both are requests from bottom to top).

    Mgr = empno PREREQUISITE or equivalent

    PRIOR empno = mgr means exactly the opposite.  When you move from level N to level N + 1 in the query, you will move to an older person, to a junior position in the hierarchy.  This is called a query from top to bottom.  The employee level N will be the Manager of wover is a level N + 1.

  • Using the e-mail from comcast and grabbing the first letter of an address name that it will display the addresses starting with that letter, now it doesn't. If I use IE9 it works correctly, or if I change preferences to Connect Lite, it will work with Fir

    Think it may have been happened after Firefox 7.0.1 update

    I guess you'll have to stick with Connect Lite until Comcast fixes the mess, they did the upgrade of Xfinity Connect. And this problem is not only to Firefox 7.0.1 from what I've seen this problem myself, I have to use Connect Lite with Firefox 3.6.23, too.

    Two years ago when they started using Zimbra, it took like 6 months to fix it if it worked in Firefox as well as he did with IE. Not surprising if MS is a main shareholder of Comcast.

  • SatelliteU300 will not start with power connected and fully charged battery

    I just bought a U300-13U. It works fine when charging the battery and AC is connected. However, if left to recharge completely it does not start with connected AC. Unpluging the power or remove the battery and running on AC make it work very well. Is a 'feature' to stop an overload or overheating of the battery, or just a problem with this machine. I want to run with AC and battery than both connected all the time like I did with all my other laptops.

    Any help appreciated.

    Hello Andy

    Of my friends has this small U300 and everything works well. I got it a few weeks ago to test the family Windows XP Setup and it works perfectly.

    Please test again very carefully and if the same thing happen again once I recommend you contact the Service partner in your country. They check your laptop. I have to be honest and say that describes the problem of strange sounds to me and please, don't wait too long. Contact the service as soon as possible.

    Bye and good luck!

  • I installed and licensed non-cloud versions of Acrobat and Photoshop, which, after several months, asking me to connect and start my trial version online. What's up with that?

    I installed and licensed non-cloud versions of Acrobat and Photoshop, which, after several months, asking me to connect and start my trial version online. What's up with that?

    even if you do not have the cc versions, follow these tips. Creative cloud applications unexpectedly back in the test mode. CS6, CCM

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

    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                   0
    To identify the hierarchical relationship of the data, which are PARENT_ACCOUNT_ID & ACCOUNT_ID, here's the query I 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
    ;
    It is the result of 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                   0
    My question is how can I changed the query to calculate the values for:

    My_Total_PR - number of my accounts to child PR which do not include himself.
    Total_PR - Total number of accounts PR in the overall structure
    My_Total_NPR - number of my accounts of child NPR which do not include himself.
    Total_NPR - Total number of accounts NPR in the overall structure

    * PR stand for responsible for payment, for example the responsible account payment 158 158 (Paying_Account_ID), therefore the Total_PR to 158 is 3 (158, 159, 160)
    * NPR stand responsible for Non-payment, for example the responsible account payment 159 is 158 (Paying_Account_ID), so the Total_NPR for 159 1

    This is the expected result, any advice appreciated. Thank you
    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              3385127
    Published by: user11432758 on February 14, 2012 01:00

    Published by: user11432758 on February 14, 2012 07:05

    Hello

    user11432758 wrote:
    Hi here is the statement DDL, thank you

    CREATE TABLE "SYSTEM"."ACCOUNT" ...
    

    Do not create your own objects in the diagram of the SYSTEM or any scheme that comes with the database. Create a separate schema and place your items. You'll have fewer security problems, and the migration to a new database will be easier.

    Here's a way to can get the aggregates 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     0
    

    This gives the correct numbers, but how can bring us in an order that reflects the hierarchy, with the columns (for example lvl) that come from the hierarchy?
    A solution would be to make two CONNECT BY queries; a service without START WITH clause (like the one above) who collects the aggregates and the other with a START WITH clause (as your original query), which is in the right order and columns such as level_label and level. We could join result sets and get exactly what we want. I'll leave that as an exercise.

    Here is another way, which gets good results with only one CONNECTION PER request:

    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    24671
    

    That's exactly what you asked for, except that you have posted the line with level_label =' 24671' before the line with level_label = "24671 '. You may not care about who comes first, but if it's important, explains why these lines should be in descending order of account_id, while "159 and 160" are in ascending order. You will need change the ORDERBY brothers and SŒURS clause accordingly.

  • Trying to understand HT203325: cannot connect to the server with the account of the user of a network

    It is a kind of home network issues.  Basically what I want to do is set up a unified environment with the profiles of school boards.  I have a couple of iMacs, a couple of the MacBook and some iPods, iPads and iPhones.

    I would like to run the server on one of my iMac, but this note from Apple shows that I'm having problems.  I don't have a machine that I can run like a dedicated server, I need all machines to be accessible by all users of the Open Directory.

    More I read the note, less I seem to understand.  Forgive me, I'm not an IT professional.  I put in place quite a while Apple (10.4 & 10.5) servers and we had no problem with network users that connect on the real server.

    It was that home directories have been shared with AFP or NFS.  I thought that I saw that they are now shared with AFP or SMB.  Note trying to tell if the server is editing on the one hand, that no other client can mount and access a share until the server has removed it?  This seems to contradict the network share point.

    Is there anyway to overcome this problem?  I tried google, but I see a lot of messages that point to the note from Apple, but not really one that helps solve the problem.  Could I run the server on the two iMacs, put on an OD and share the home directories of the other?  So I think that it would be the base directory is never hosted on the same computer where the authentication takes place.

    Any clarification would be appreciated.

    Thank you

    Brett

    PS: My Mac is fairly new, so I can update all 10.11.2 & I didn't buy server, so I would get the most recent and for that, too.

    Try it.   But I wouldn't expect that you are trying to do will be reliable, and I would not recommend using a server as an interactive workstation - the two tasks are usually in opposite directions.

    If you want to store shared files, that activate the OS X client, or install and use a NAS box.   There is no server need, if networked storage is the goal.

    Running a server means implementation of DNS services locally and many other tasks.   It was possible to get out for the most part without doing that, but 10.5 started to get flaky without services local DNS and security and network authentication are now commonplace, and this does not at all well without local DNS.

    Servers should also be available at all times, and - as often happens - shared systems may be rather less stable that software is modified, the systems are closed down, or otherwise.

    NFS is available on OS X for several years, and some people have manually configured access remotely via NFS.   It is much more common to see the AFP and increasingly used CIFS/SMB, however.

    Of HT203325: users can be local, as is typical of most of the OS X client configurations, or they can be so-called network users - users with their connection directory located elsewhere and often with access and the same password on multiple systems.   If users are configured as users of the server network, then do not allow users to connect directly to the server.   Who - is the server that serves files and directories of itself to itself - is not reliable.  Not in this technical note, and not by what I saw.

Maybe you are looking for

  • Blocks of FF Java 7 day 45. Why? Explanation regards 7U11.

    Installed upgrading Java 7 45.Have the two 32-bit versions and 64-bit installed from Oracle's Web site. Works in Chrome. 24.0 Firefox displays the error message indicating that the Java plugin has known vulnerabilities. On the support page, it has a

  • is there a way to transfer my bookmarks from IE firefox

    I want to transfer my i.e. bookmarks in my firefox browser. Is there a simple way to do this?

  • Tecra A10 with wrong source IP address ping responses

    Hello! I have problems with my network connection. After installing Win7 pro answers tecra A10 ping on the lan with 192.168.0.239 port, but he 192.168.211.36 the DHCP server.In Wireshark on tecra A10 I don't see demand for IGMP but no answer. On the

  • The SW1730 upgrade with DVD/RW drive

    Any ideas on the compatibility of the dvd rw disks for my long in the tooth s1730. Would be a work of different manufacturers drive or another drive toshiba? Any information would be appreciated.

  • Satellite P300-18 is not always launch windows

    McAfee Anti-virus on my P300 18 bypassed Downloader Trojan horse.Eventually got rid of it after the purchase of Malwarebytes Pro. Since that time my machine; while starting, is not always launch windows on the first attempt and must be stopped or sta