I am new in Oracle, I am going to use connect by prior to implement the flat table instead of Hierarchical one. but I am a little bit confuse. My table is like this: empTabl:
| empID | empName | managerID |
|---|---|---|
| 100 | Sara | 110 |
| 101 | Ben | 111 |
| 102 | Alex | 110 |
| 110 | Ross | 111 |
| 111 | Mon | NULL |
I am going to change the table like this(output):
| emp | empName | subBoss | subBossName | Boss | BossName |
|---|---|---|---|---|---|
| 100 | Sara | 110 | Ross | 111 | Mon |
| 101 | Ben | 111 | Mon | NULL | NULL |
| 102 | Alex | 110 | Ross | 111 | Mon |
| 110 | Ross | 111 | Mon | NULL | NULL |
| 111 | Mon | NULL | NULL | NULL | NULL |
You can use a recursive sub-query factoring clause for this:
Or, you can use a hierarchical query and pivot:
Which, for the sample data:
Both output:
db<>fiddle here