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