Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

user11432758Feb 14 2012 — edited Feb 17 2012
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, 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                   0
My 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              3385127
Edited by: user11432758 on 14-Feb-2012 01:00

Edited by: user11432758 on 14-Feb-2012 07:05
This post has been answered by Frank Kulash on Feb 14 2012
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 16 2012
Added on Feb 14 2012
11 comments
3,073 views