Oracle
 sql >> база данни >  >> RDS >> Oracle

Как да получите пътя на йерархична таблица

Настройка на Oracle :

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;

Запитване :

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
);

Изход :

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                                                                   


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Стъпки за синхронизиране в режим на готовност с първична база данни в Oracle

  2. Oracle - Как да генерирам скрипт от sql разработчик

  3. php oci8 ext не се зарежда Не може да се зареди динамична библиотека

  4. Създаване на тригери

  5. NUMTODSINTERVAL() Функция в Oracle