I try to get a result like this:
10 Pos 1 = 20
---------------Pos 1
---------------Pos 2 = 50
---------------------------Pos 1
Pos 2 = 30Pos 3 = 40
---------------Pos 1
---------------Pos 2
CREATE TABLE beispiel (
fa INTEGER,
fa_pos integer,
verurs INTEGER,
verurs_pos INTEGER,
auftrag INTEGER
);
INSERT INTO beispiel VALUES ( 10 , 1 , 24548 , 1 , 24548 );
INSERT INTO beispiel VALUES ( 10 , 2 , 24548 , 1 , 24548 );
INSERT INTO beispiel VALUES ( 10 , 3 , 24548 , 1 , 24548 );
INSERT INTO beispiel VALUES ( 20 , 1 , 10 , 1 , 24548 );
INSERT INTO beispiel VALUES ( 20 , 2 , 10 , 1 , 24548 );
INSERT INTO beispiel VALUES ( 30 , 0 , 10 , 2 , 24548 );
INSERT INTO beispiel VALUES ( 40 , 1 , 10 , 3 , 24548 );
INSERT INTO beispiel VALUES ( 40 , 2 , 10 , 3 , 24548 );
INSERT INTO beispiel VALUES ( 50 , 1 , 20 , 2 , 24548 );
I run this statement:
SELECT
LEVEL, sys_connect_by_path(fa, "/") AS pfad,
fa, fa_pos, verurs, verurs_pos
FROM beispiel
CONNECT BY PRIOR fa = verurs
GROUP BY LEVEL,fa, pfad, fa_pos, verurs, verurs_pos
ORDER siblings BY verurs
Result:
level pfad fa fa_pos verurs verurs_pos
1 /10 10 1 24548 1
2 /10/20 20 2 10 1
2 /10/20 20 1 10 1
3 /10/20/50 50 1 20 2
2 /10/30 30 0 10 2
1 /40 40 1 10 3
1 /50 50 1 20 2
1 /20 20 1 10 1
1 /30 30 0 10 2
2 /10/40 40 1 10 3
1 /10 10 3 24548 1
1 /10 10 2 24548 1
2 /10/40 40 2 10 3
2 /20/50 50 1 20 2
1 /20 20 2 10 1
1 /40 40 2 10 3
Why do I get as result /50? How can I ignore this value? And I get duplicated values and the Tree doesn´t look like a Child --> Parent --> Grandparent Structure.
Do I really need to do a GROUP BY?
Any idea?
I need to get an ordered result with siblings.
You can modify the
CONNECT BYclause and use theCONNECT_BY_ROOTfunction in your SQL query. Additionally, you can use theORDER SIBLINGS BYclause to order the result by specific columns. Here's the modified SQL query:Add
CONNECT_BY_ROOT(fa) AS root_faandCONNECT_BY_ROOT(fa_pos) AS root_fa_posto capture the root values forfaandfa_posfor each branch in the hierarchical structure.The
CONNECT BYclause is modified to includeAND PRIOR auftrag = auftragto ensure that the hierarchy is connected based on theauftragcolumn as well. This way, each branch will only contain values related to the sameauftrag.Use the
ORDER SIBLINGS BYclause to specify the order of siblings in the hierarchy. In this case, we order them first byverurs_posand then byfa_pos.With these modifications, you should get the desired ordered result without duplicates, and the tree structure will represent the Child -> Parent -> Grandparent relationship based on
faandverurscolumns within the sameauftrag.