I'm trying to create a query to transform a hierarchical table structure to columns.
Each Node has a parent. But the Node is not a unique number. In each root-leaf it is unique. But can exist under an other parent.
I tried with a CTE. And with multiple SELECT .. UNION ...
But I'm unable to filter out that it is in the same root-leaf. For example, I'm getting the two selected lines for both parent 01 as 09.
The max depth is 4.
And the result I want is. RUBRICCODE;NAME0;NAME1;NAME2;NAME3;NAME4 0100000000;Financieel Administratieve Documenten;;;; 0100100000;Financieel Administratieve Documenten;Patientenadministratie;;; ...
select * from (
select t0.RUBRICCODE, t0.PATH, t0.NAME Rubr0, '' Rubr1, '' Rubr2, '' Rubr3, '' Rubr4
from dacs_treestructure t0
where t0.DEPTH = 0
UNION ALL
select t1.RUBRICCODE, t1.PATH, t0.NAME Rubr0, t1.NAME Rubr1, '' Rubr2, '' Rubr3, '' Rubr4
from dacs_treestructure t0
join dacs_treestructure t1 on t0.NODE = t1.PARENT
where t0.DEPTH = 0 and t1.DEPTH = 1
UNION ALL
select t2.RUBRICCODE, t2.PATH, t0.NAME Rubr0, t1.NAME Rubr1, t2.NAME Rubr2, '' Rubr3, '' Rubr4
from dacs_treestructure t0
join dacs_treestructure t1 on t0.NODE = t1.PARENT
join dacs_treestructure t2 on t1.NODE = t2.PARENT
where t0.DEPTH = 0 and t1.DEPTH = 1 and t2.DEPTH = 2
) x order by 1
Result of the query. The selected lines are from parent 09. And should not exist.