How to get the path of an hierarchy table

家住魔仙堡 提交于 2019-12-13 08:57:15

问题


I've been struggling a bit about how to deal with this situation:

I have a table structured as follow:

Family_code  |   Parent_Family_Code  | ....
    1                   2
    2                   4
    3                   6
    4                   3
   ......................

When a user is searching for a specific family code, I need to return the entire path (up to 10 levels max) , so for example for family_code = 1 I'll need:

Family_code | parent_1 | p_2 | p_3 | p_4 | p_5 | .....
      1          2        4     3     6     null    null.....

I know I can use sys_connect_by_path() which will bring me the expected result but as a string, and not as separate columns which is something I'll prefer to avoid.

This can also be done with 10 left joins to the same table, or the use of LEAD()/LAG() functions which will include a lot of sub queries and will make a messy and unreadable query, but then again, this will be more heavy then it should be and I need to simplify it as I can.

I've come up with a solution using substr() function(the length of the codes will always be varchar2(3)):

SELECT s.family_code,
 s.parent_family_code_1,
 s.parent_family_code_2,
 CASE WHEN length(s.family_path) - (4 * 3 + 2) > 0 THEN substr(s.family_path, length(s.family_path) - (4 * 3 + 2), 3) ELSE NULL END as parent_family_code_3,
 CASE WHEN length(s.family_path) - (4 * 4 + 2) > 0 THEN substr(s.family_path, length(s.family_path) - (4 * 4 + 2), 3) ELSE NULL END as parent_family_code_4,
 CASE WHEN length(s.family_path) - (4 * 5 + 2) > 0 THEN substr(s.family_path, length(s.family_path) - (4 * 5 + 2), 3) ELSE NULL END as parent_family_code_5,
 CASE WHEN length(s.family_path) - (4 * 6 + 2) > 0 THEN substr(s.family_path, length(s.family_path) - (4 * 6 + 2), 3) ELSE NULL END as parent_family_code_6,
 CASE WHEN length(s.family_path) - (4 * 7 + 2) > 0 THEN substr(s.family_path, length(s.family_path) - (4 * 7 + 2), 3) ELSE NULL END as parent_family_code_7,
 CASE WHEN length(s.family_path) - (4 * 8 + 2) > 0 THEN substr(s.family_path, length(s.family_path) - (4 * 8 + 2), 3) ELSE NULL END as parent_family_code_8,
 CASE WHEN length(s.family_path) - (4 * 9 + 2) > 0 THEN substr(s.family_path, length(s.family_path) - (4 * 9 + 2), 3) ELSE NULL END as parent_family_code_9,
 CASE WHEN length(s.family_path) - (4 * 10 + 2) > 0 THEN substr(s.family_path, length(s.family_path) - (4 * 10 + 2), 3) ELSE NULL END as parent_family_code_10
  FROM (SELECT t.family_code,
               t.parent_family_code as parent_family_code_1,
               prior t.parent_family_code as parent_family_code_2,
               sys_connect_by_path(t.family_code, ',') as family_path
          FROM table t
        connect by prior t.family_code = t.parent_family_code) s

But I would like a solution without the use of substrings since it will be harder to do any maintaince on it when other developers will touch it . So basically my question is - how do I select the entire path as different columns without the use of substrings?


回答1:


A slightly modified query from @MT0 answer, using PIVOT clause.

SELECT * 
FROM (
    select connect_by_root( family_code ) as Family_code, 
           'P_' || level lev_el,  
           parent_family_code
    from table_name t
    start with not exists(
        select 1 from table_name t1
        where t.family_code = t1.parent_family_code )
    connect by prior parent_family_code =  family_code
)
PIVOT (
  max( parent_family_code ) 
  FOR (lev_el) IN ( 
       'P_1', 'P_2', 'P_3', 'P_4', 'P_5', 'P_6','P_7', 'P_8','P_9','P_10' ,
       'P_11', 'P_12', 'P_13', 'P_14', 'P_15', 'P_16','P_17', 'P_18','P_19','P_20',
       'P_21', 'P_22', 'P_23', 'P_24', 'P_25', 'P_26','P_27', 'P_28','P_29','P_30' 
       /* add more "levels" here if required */
)
);

A result of the query for sample data from @MT0 answer (@MT0, thank you for providing sample data):

    FAMILY_CODE      'P_1'      'P_2'      'P_3'      'P_4'      'P_5'      'P_6'      'P_7'      'P_8'      'P_9'     'P_10'     'P_11'     'P_12'     'P_13'     'P_14'     'P_15'     'P_16'     'P_17'     'P_18'     'P_19'     'P_20'     'P_21'     'P_22'     'P_23'     'P_24'     'P_25'     'P_26'     'P_27'     'P_28'     'P_29'     'P_30'
--------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
              1          2          4          5          6                                                                                                                                                                                                                                                                                              
              8          7          9         10         11                                                                                                                                                                                                                                                                                              



回答2:


Oracle Setup:

CREATE TABLE table_name ( Family_code, Parent_Family_Code ) AS
SELECT  1,    2 FROM DUAL UNION ALL
SELECT  2,    4 FROM DUAL UNION ALL
SELECT  3,    6 FROM DUAL UNION ALL
SELECT  6, NULL FROM DUAL UNION ALL
SELECT  4,    3 FROM DUAL UNION ALL
SELECT  4,    5 FROM DUAL UNION ALL
SELECT  5, NULL FROM DUAL UNION ALL
SELECT  8,    7 FROM DUAL UNION ALL
SELECT  7,    9 FROM DUAL UNION ALL
SELECT  9,   10 FROM DUAL UNION ALL
SELECT 10,   11 FROM DUAL UNION ALL
SELECT 11, NULL FROM DUAL;

Query:

SELECT TO_NUMBER( REGEXP_SUBSTR( path, '/(\d+)', 1, max_depth, NULL, 1 ) ) AS family_code,
       CASE WHEN max_depth >  1 THEN TO_NUMBER( REGEXP_SUBSTR( path, '/(\d+)', 1, max_depth -  1, NULL, 1 ) ) END AS p1,
       CASE WHEN max_depth >  2 THEN TO_NUMBER( REGEXP_SUBSTR( path, '/(\d+)', 1, max_depth -  2, NULL, 1 ) ) END AS p2,
       CASE WHEN max_depth >  3 THEN TO_NUMBER( REGEXP_SUBSTR( path, '/(\d+)', 1, max_depth -  3, NULL, 1 ) ) END AS p3,
       CASE WHEN max_depth >  4 THEN TO_NUMBER( REGEXP_SUBSTR( path, '/(\d+)', 1, max_depth -  4, NULL, 1 ) ) END AS p4,
       CASE WHEN max_depth >  5 THEN TO_NUMBER( REGEXP_SUBSTR( path, '/(\d+)', 1, max_depth -  5, NULL, 1 ) ) END AS p5,
       CASE WHEN max_depth >  6 THEN TO_NUMBER( REGEXP_SUBSTR( path, '/(\d+)', 1, max_depth -  6, NULL, 1 ) ) END AS p6,
       CASE WHEN max_depth >  7 THEN TO_NUMBER( REGEXP_SUBSTR( path, '/(\d+)', 1, max_depth -  7, NULL, 1 ) ) END AS p7,
       CASE WHEN max_depth >  8 THEN TO_NUMBER( REGEXP_SUBSTR( path, '/(\d+)', 1, max_depth -  8, NULL, 1 ) ) END AS p8,
       CASE WHEN max_depth >  9 THEN TO_NUMBER( REGEXP_SUBSTR( path, '/(\d+)', 1, max_depth -  9, NULL, 1 ) ) END AS p9,
       CASE WHEN max_depth > 10 THEN TO_NUMBER( REGEXP_SUBSTR( path, '/(\d+)', 1, max_depth - 10, NULL, 1 ) ) END AS p10
FROM   (
  SELECT SYS_CONNECT_BY_PATH( Family_code, '/' ) AS path,
         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                                                                   


来源:https://stackoverflow.com/questions/36964508/how-to-get-the-path-of-an-hierarchy-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!