I have a table with this structure:
- ELEMENT
- FATHER_ELEMENT
- INITIAL_DATE
- END_DATE
I can have this situation:
| ELEMENT| FATHER_ELEMENT|
|--------|---------------|
| A | B |
| A | C |
| B | D |
| D | E |
| X | Y |
And I'd like to have this output:
| LIV0| LIV1|LIV2| LIV3| LIV4|
|-----|-----|----|-----|-----|
| A | B |D |E |E |
| A | C |C |C |C |
| B | D |D |D |D |
| X | Y |Y |Y |Y |
Basically I want to generate some rows starting from all the fathers and having 1 record for each path until I reach a different leaf, I want it using 12 level (in the example there are 5 levels) and, If for example, for a path, I have deep = 4, the output should have replicate the leaf until the last level is reached.
How can I obtain that result?
Thank you
You can use a recursive sub-query factoring clause:
Which, for the sample data:
Outputs:
db<>fiddle here