Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Tree Structure Hierarchy Data Display in Single Row Different Column

I have a hierarchical table named Employee_Hierarchy in oracle with column names entity_code, parent_entity_code, entity_name and entity_role with no cycle. Having lower most child in other table named Client connected with hierarchical table's lower most child with entity_code. I have to display the data in single row hierarchy where column name will be appended by role.

Sample Example:

Tree Structure:

enter image description here

Hierarchical Table:

enter image description here

Lower Most Child's Table:

enter image description here

Expected Result:

enter image description here

Is there a way to get the expected result through oracle query? And the expected result depends on input that means it will not always start from root element, it may start from any node for example team-lead (Shail) to lowermost child.

(Note:If a upper hierachy is missing then the parent_code of the current node will be its upper hierarchy's parent_code And the missing hierarchy element will be blank in expected result.)

Thanks in Advance.

like image 321
Shailesh Yadav Avatar asked Feb 22 '17 14:02

Shailesh Yadav


People also ask

What is Sys_connect_by_path function in Oracle?

SYS_CONNECT_BY_PATH is valid only in hierarchical queries. It returns the path of a column value from root to node, with column values separated by char for each row returned by CONNECT BY condition. Both column and char can be any of the datatypes CHAR , VARCHAR2 , NCHAR , or NVARCHAR2 .

What does column (+) mean in Oracle?

The plus sign is Oracle syntax for an outer join. There isn't a minus operator for joins. An outer join means return all rows from one table. Also return the rows from the outer joined where there's a match on the join key. If there's no matching row, return null.

How do you display table structure in Oracle?

For a list of tables in the current schema, use the Show Tables command. For a list of views in the current schema, use the Show Views command. For a list of available schemas, use the Show Schemas command. If the table or view is in a particular schema, qualify it with the schema name.

How do you concatenate in PL SQL?

The CONCAT function allows you to concatenate two strings together. To CONCAT more than two values, we can nest multiple CONCAT function calls. Parameters Used: string1: It is used to specify the first string to concatenate.


1 Answers

select      h.Manager_entity_code  
           ,h.Manager_entity_name  
           ,h.Team_Lead_entity_code
           ,h.Team_Lead_entity_name
           ,h.Developer_entity_code
           ,h.Developer_entity_name
           ,c.client_name

from       (select      trim (both ',' from sys_connect_by_path (case when entity_role = 'Manager'   then entity_code end,','))  as Manager_entity_code
                       ,trim (both ',' from sys_connect_by_path (case when entity_role = 'Manager'   then entity_name end,','))  as Manager_entity_name
                       ,trim (both ',' from sys_connect_by_path (case when entity_role = 'Team-Lead' then entity_code end,','))  as Team_Lead_entity_code
                       ,trim (both ',' from sys_connect_by_path (case when entity_role = 'Team-Lead' then entity_name end,','))  as Team_Lead_entity_name
                       ,trim (both ',' from sys_connect_by_path (case when entity_role = 'Developer' then entity_code end,','))  as Developer_entity_code
                       ,trim (both ',' from sys_connect_by_path (case when entity_role = 'Developer' then entity_name end,','))  as Developer_entity_name

            from        hierarchical_table

            where       connect_by_isleaf = 1

            connect by  parent_entity_code = prior entity_code

            start with  entity_code = 100  
            ) h

            join client_table c

            on   c.entity_code  =
                 h.Developer_entity_code 

order by    h.Manager_entity_code  
           ,h.Team_Lead_entity_code
           ,h.Developer_entity_code
;

+---------------------+---------------------+-----------------------+-----------------------+-----------------------+-----------------------+-------------+
| MANAGER_ENTITY_CODE | MANAGER_ENTITY_NAME | TEAM_LEAD_ENTITY_CODE | TEAM_LEAD_ENTITY_NAME | DEVELOPER_ENTITY_CODE | DEVELOPER_ENTITY_NAME | CLIENT_NAME |
+---------------------+---------------------+-----------------------+-----------------------+-----------------------+-----------------------+-------------+
|                 100 | Mack                |                   200 | Shail                 |                   500 | Neha                  | Tata        |
|                 100 | Mack                |                   300 | Jack                  |                   600 | Rocky                 | Rel         |
|                 100 | Mack                |                   300 | Jack                  |                   600 | Rocky                 | Voda        |
|                 100 | Mack                |                   300 | Jack                  |                   600 | Rocky                 | Airtel      |
+---------------------+---------------------+-----------------------+-----------------------+-----------------------+-----------------------+-------------+  

For start with entity_code = 300

+---------------------+---------------------+-----------------------+-----------------------+-----------------------+-----------------------+-------------+
| MANAGER_ENTITY_CODE | MANAGER_ENTITY_NAME | TEAM_LEAD_ENTITY_CODE | TEAM_LEAD_ENTITY_NAME | DEVELOPER_ENTITY_CODE | DEVELOPER_ENTITY_NAME | CLIENT_NAME |
+---------------------+---------------------+-----------------------+-----------------------+-----------------------+-----------------------+-------------+
| (null)              | (null)              |                   300 | Jack                  |                   600 | Rocky                 | Airtel      |
| (null)              | (null)              |                   300 | Jack                  |                   600 | Rocky                 | Voda        |
| (null)              | (null)              |                   300 | Jack                  |                   600 | Rocky                 | Rel         |
+---------------------+---------------------+-----------------------+-----------------------+-----------------------+-----------------------+-------------+
like image 74
David דודו Markovitz Avatar answered Oct 21 '22 20:10

David דודו Markovitz