Question : Oracle SQL: Propagate Parent value to ALL Children during Tree Walk

Ok so here is my problem.

I have a table with a self referencing join (pigs ear) that essentially stores a Windows directory structure:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
CREATE TABLE DIRECTORY 
(
 CHILD_FOLDER VARCHAR2 (100)
,PARENT_FOLDER VARCHAR2 (100)
,PERMISSION VARCHAR2 (100);
);

INSERT INTO DIRECTORY (CHILD_FOLDER, PARENT_FOLDER) VALUES ('COMPANY_HQ', NULL, 'F');
INSERT INTO DIRECTORY (CHILD_FOLDER, PARENT_FOLDER) VALUES ('HR', 'COMPANY_HQ');
INSERT INTO DIRECTORY (CHILD_FOLDER, PARENT_FOLDER) VALUES ('IT', 'COMPANY_HQ');
INSERT INTO DIRECTORY (CHILD_FOLDER, PARENT_FOLDER) VALUES ('FINANCE', 'COMPANY_HQ');
INSERT INTO DIRECTORY (CHILD_FOLDER, PARENT_FOLDER) VALUES ('DEVELOPMENT', 'IT', 'W');
INSERT INTO DIRECTORY (CHILD_FOLDER, PARENT_FOLDER) VALUES ('TEST', 'IT');
INSERT INTO DIRECTORY (CHILD_FOLDER, PARENT_FOLDER) VALUES ('SUPPORT', 'IT');


A quick tree walk gives me the corresponding structure:

1:
2:
3:
4:
select lpad(' ',level*2,' ') || child_folder structure, permission
from directory
START WITH PARENT_FOLDER IS NULL
CONNECT BY PRIOR CHILD_FOLDER = PARENT_FOLDER;


1:
2:
3:
4:
5:
6:
7:
8:
9:
STRUCTURE     PERMISSION
-------------------------------------------------
  COMPANY_HQ          F
    FINANCE
    HR
    IT
      DEVELOPMENT    W
      SUPPORT
      TEST

What I would like to achieve via SQL (and I am pretty sure the Analytics will have to be used) is the follows:

1. Each entry in the DIRECTORY table should propagate the value of it's permission down to ALL child units. Note that the Root level of "COMPANY_HQ" has the permission value of "F" and the grandchild entry of "DEVELOPMENT" has the permission value of "W" and these are the only permission values stored. The result needs to look like follows:

STRUCTURE                  PERMISSION
-------------------------------------------------
  COMPANY_HQ            F
    FINANCE                  (F)
    HR                        (F)
    IT                              (F)
      DEVELOPMENT          (F)W
      SUPPORT                  (F)(W)
      TEST                        (F)(W)

As you can see, the values of (F) of (W) indicate that the value has been inherited from above (COMPANY_HQ and DEVELOPMENT respectively).

So I guess my question is within the construct of SQL, is it possible to propagate a value from a parent down to all it's children?

Hope someone can help :)

Duncs

Answer : Oracle SQL: Propagate Parent value to ALL Children during Tree Walk

start by using
select ... , permission,
SYS_CONNECT_BY_PATH(permission,';')
FROM
 and find a way to format your result removing duplicate values and adding the brackets
Random Solutions  
 
programming4us programming4us