Oracle-opsætning :
OPRET TABEL tabelnavn ( Family_code, Parent_Family_Code ) ASSELECT 1, 2 FRA DUAL UNION ALLSELECT 2, 4 FRA DUAL UNION ALLSELECT 3, 6 FRA DUAL UNION ALLSELECT 6, NULL FRA DUAL UNION ALLSELECTROM VALG 4, 4, 5 FRA DUAL UNION ALLSELECT 5, NULL FRA DUAL UNION ALLSELECT 8, 7 FRA DUAL UNION ALLSELECT 7, 9 FRA DUAL UNION ALLSELECT 9, 10 FRA DUAL UNION ALLSELECT 10, 11 FRA DUAL UNION ALLSELECT FROM 11;11 kode>
Forespørgsel :
SELECT TO_NUMBER( REGEXP_SUBSTR( sti, '/(\d+)', 1, max_depth, NULL, 1 ) ) AS family_code, CASE WHEN max_depth> 1 THEN TO_NUMBER( REGEXP_SUBSTR( sti, '/(\d+) )', 1, max_depth - 1, NULL, 1 ) ) END AS p1, CASE WHEN max_depth> 2 THEN TO_NUMBER( REGEXP_SUBSTR( sti, '/(\d+)', 1, max_depth - 2, NULL, 1 ) )END AS p2, CASE WHEN max_depth> 3 THEN TO_NUMBER( REGEXP_SUBSTR( sti, '/(\d+)', 1, max_depth - 3, NULL, 1 ) ) END AS p3, CASE NÅR max_depth> 4 THEN TO_NUMBER( REGEXP_SUBSTR(sti) '/(\d+)', 1, max_depth - 4, NULL, 1 ) ) END AS p4, CASE WHEN max_depth> 5 THEN TO_NUMBER( REGEXP_SUBSTR( sti, '/(\d+)', 1, max_depth - 5, NULL , 1 ) ) END SOM p5, CASE NÅR max_depth> 6 THEN TO_NUMBER( REGEXP_SUBSTR( sti, '/(\d+)', 1, max_depth - 6, NULL, 1 ) ) END AS p6, CASE NÅR max THE_DEpth TO_NUMBER> 7 ( REGEXP_SUBSTR( sti, '/(\d+)', 1, max_depth - 7, NULL, 1 ) ) END AS p7, CASE WHEN max_depth> 8 THEN TO_NUMBER( REGEXP_SUBS TR( sti, '/(\d+)', 1, max_depth - 8, NULL, 1 ) ) END AS p8, CASE WHEN max_depth> 9 THEN TO_NUMBER( REGEXP_SUBSTR( sti, '/(\d+)', 1, max_depth - 9, NULL, 1 ) ) END SOM p9, CASE WHEN max_depth> 10 THEN TO_NUMBER( REGEXP_SUBSTR( sti, '/(\d+)', 1, max_depth - 10, NULL, 1 ) ) END AS p10FROM_PATHNECT_BYYS_CON Family_code, '/' ) AS sti, LEVEL AS max_depth FROM table_name WHERE CONNECT_BY_ISLEAF =1 CONNECT BY PRIOR Family_Code =Parent_Family_Code START WITH Parent_Family_Code IS NULL);
Output :
FAMILY_CODE P1 P2 P3 P4 P5 P6 P7 P8 P9 P10----- ---------- ---------- -- ---------- ---------- ---------- ---------- -- -------- ---------- ---------- 1 2 4 5 1 2 4 3 6 8 7 9 10 11