分层查询

时间:2016-07-24 10:01:25

标签: sql oracle hierarchy

我已经创造了某种依赖关系"包含我们夜间进程的整个依赖关系的表。

表格如下:

GRAND_MODEL | WAIT_4_MODEL_NAME
test            test1
test            test2
test            test3
test2           test3
test3           test4
test4           test5

此表表示 - > test需要等待test1test2test3才能完成,但还需要等待test4test5,因为test3/4等待他们。 test1并不等待任何事情,test2等待test3,因此test4也因此test5

所以结果应该是这样的:

FIRST_MODEL | SECOND_MODEL | THIRD_MODEL | FORTH_MODEL | FIFTH_MODEL | SIXTH_MODEL
 test5           test4            test3       test2          test        NULL 
 test5           test4            test3       test           NULL        NULL
 test4           test3            test2       test           NULL        NULL
.................................

我尝试过的事情:

SELECT distinct prior wait_4_model_name as first_m,
                wait_4_model_name as second_m,
                grand_model as third_m
  from (SELECT distinct grand_model, wait_4_model_name
          FROM DEL_SAGI_FOR_HIERARCHY)
connect by NOCYCLE prior grand_model = wait_4_model_name

但这只会生成层次结构的第一级。

提前致谢。

编辑:请注意,可能存在相反的依赖关系,test等待test1test1等待test(每个模型都很大) ,因此模型的一部分可以等待另一个模型的一部分)

2 个答案:

答案 0 :(得分:2)

Oracle安装程序

CREATE TABLE table_name ( GRAND_MODEL, WAIT_4_MODEL_NAME ) AS
SELECT 'test',  'test1' FROM DUAL UNION ALL
SELECT 'test',  'test2' FROM DUAL UNION ALL
SELECT 'test',  'test3' FROM DUAL UNION ALL
SELECT 'test2', 'test3' FROM DUAL UNION ALL
SELECT 'test3', 'test4' FROM DUAL UNION ALL
SELECT 'test4', 'test5' FROM DUAL;

<强>查询

SELECT REGEXP_SUBSTR( tests, '[^|]+', 1, 1 ) AS first_model,
       REGEXP_SUBSTR( tests, '[^|]+', 1, 2 ) AS second_model,
       REGEXP_SUBSTR( tests, '[^|]+', 1, 3 ) AS third_model,
       REGEXP_SUBSTR( tests, '[^|]+', 1, 4 ) AS fourth_model,
       REGEXP_SUBSTR( tests, '[^|]+', 1, 5 ) AS fifth_model,
       REGEXP_SUBSTR( tests, '[^|]+', 1, 6 ) AS sixth_model,
       REGEXP_SUBSTR( tests, '[^|]+', 1, 7 ) AS seventh_model,
       REGEXP_SUBSTR( tests, '[^|]+', 1, 8 ) AS eighth_model,
       REGEXP_SUBSTR( tests, '[^|]+', 1, 9 ) AS ninth_model
FROM   (
  SELECT SYS_CONNECT_BY_PATH( wait_4_model_name, '|' ) || '|' ||  grand_model AS tests
  FROM  table_name
  CONNECT BY PRIOR grand_model = wait_4_model_name
);

<强>输出

FIRST_MODEL SECOND_MODEL THIRD_MODEL FOURTH_MODEL FIFTH_MODEL SIXTH_MODEL SEVENTH_MODEL EIGHTH_MODEL NINTH_MODEL
----------- ------------ ----------- ------------ ----------- ----------- ------------- ------------ -----------
test1       test                                                                                                 
test2       test                                                                                                 
test3       test                                                                                                 
test3       test2                                                                                                
test3       test2        test                                                                                    
test4       test3                                                                                                
test4       test3        test                                                                                    
test4       test3        test2                                                                                   
test4       test3        test2       test                                                                        
test5       test4                                                                                                
test5       test4        test3                                                                                   
test5       test4        test3       test                                                                        
test5       test4        test3       test2                                                                       
test5       test4        test3       test2        test                                                           

答案 1 :(得分:1)

这是一个不需要字符串连接和重新拆分的解决方案:

select    a.wait_4_model_name model1,
          a.grand_model       model2,
          b.grand_model       model3,
          c.grand_model       model4,
          d.grand_model       model5,
          e.grand_model       model6
from      DEL_SAGI_FOR_HIERARCHY a
left join DEL_SAGI_FOR_HIERARCHY b ON a.grand_model = b.wait_4_model_name
left join DEL_SAGI_FOR_HIERARCHY c ON b.grand_model = c.wait_4_model_name
left join DEL_SAGI_FOR_HIERARCHY d ON c.grand_model = d.wait_4_model_name
left join DEL_SAGI_FOR_HIERARCHY e ON d.grand_model = e.wait_4_model_name
where     a.wait_4_model_name not in (
              select grand_model from DEL_SAGI_FOR_HIERARCHY)
order by  1, 2, 3, 4, 5, 6

样本数据的输出是:

+--------+--------+--------+--------+--------+--------+
| MODEL1 | MODEL2 | MODEL3 | MODEL4 | MODEL5 | MODEL6 |
+--------+--------+--------+--------+--------+--------+
| test1  | test   |    -   |   -    |   -    |   -    |
| test5  | test4  | test3  | test   |   -    |   -    |
| test5  | test4  | test3  | test2  | test   |   -    |
+--------+--------+--------+--------+--------+--------+

请注意,您的示例数据没有完全没有任何依赖关系的模型实例,即不需要等待其他模型,也不需要任何模型才能启动。