2017-08-16 27 views
0

1行の出力、iは、階層データが、その行に来るを取得したい: テーブル構造:階層の階層クエリは、iを1つの階層テーブルが

id  parent_type parent_id  
    71123 FSE   67114  
    67114 DST   67111   
    67111 null   0  

出力:

id  parent_type parent_id level SYS_CONNECT_BY_PATH(parent_type, '->') 
71123 FSE   67114  1  ->FSE 
67114 DST   67111  2  ->FSE->DST 
67111 null   0  3  ->FSE->DST-> 

I WANT RESULT OF ABOVE OUTPUT AS SHOWN BELOW: 
id parent_type parent_id id parent_type parent_id id parent_type parent_id 
71123 FSE  67114  67114 DST  67111  67111 NULL 0 
+0

実際にはどうしたらいいですか? –

+0

kya apko ye html私はカルナイハイを見る? –

+0

['pivot'](http://docs.oracle.com/database/121/SQLRF/statements_10002.htm#CHDFAFIE)が必要ですが、いくつのレベルを処理しなければならないかも知っていますか?たぶんあなたは、代わりに表示するためにそれを行うことができるレポートまたはアプリケーション層を持っています。 –

答えて

0

複雑すぎるかもしれませんが、実際は簡単です。 PARENT_TYPE_LEVEL_1 | | PARENT_ID_LEVEL_1 | ID_LEVEL_2 | PARENT_TYPE_LEVEL_2 | PARENT_ID_LEVEL_2 | ID_LEVEL_3 | PARENT_TYPE_LEVEL_3 | PARENT_ID_LEVEL_3 | ID_LEVEL_4 | PARENT_TYPE_LEVEL_4 | PARENT_ID_LEVEL_4 | ID_LEVEL_5 | PARENT_TYPE_LEVEL_5 | PARENT_ID_LEVEL_5 71123 |ここ

WITH dat 
    AS (SELECT 71123 AS id, 
       'FSE' AS parent_type, 
       67114 AS parent_id 
     FROM DUAL 
     UNION 
     SELECT 67114 AS id, 
       'DST' AS parent_type, 
       67111 AS parent_id 
     FROM DUAL 
     UNION 
     SELECT 67111 AS id, 
       NULL AS parent_type, 
       0 AS parent_id 
     FROM DUAL) 
SELECT MAX (id_level_1) AS id_level_1, 
     MAX (parent_type_level_1) AS parent_type_level_1, 
     MAX (parent_id_level_1) AS parent_id_level_1, 
     MAX (id_level_2) AS id_level_2, 
     MAX (parent_type_level_2) AS parent_type_level_2, 
     MAX (parent_id_level_2) AS parent_id_level_2, 
     MAX (id_level_3) AS id_level_3, 
     MAX (parent_type_level_3) AS parent_type_level_3, 
     MAX (parent_id_level_3) AS parent_id_level_3, 
     MAX (id_level_4) AS id_level_4, 
     MAX (parent_type_level_4) AS parent_type_level_4, 
     MAX (parent_id_level_4) AS parent_id_level_4, 
     MAX (id_level_5) AS id_level_5, 
     MAX (parent_type_level_5) AS parent_type_level_5, 
     MAX (parent_id_level_5) AS parent_id_level_5 
FROM (SELECT CASE WHEN l = 1 THEN id ELSE NULL END AS id_level_1, 
       CASE WHEN l = 1 THEN parent_type ELSE NULL END AS parent_type_level_1, 
       CASE WHEN l = 1 THEN parent_id ELSE NULL END AS parent_id_level_1, 
       CASE WHEN l = 2 THEN id ELSE NULL END AS id_level_2, 
       CASE WHEN l = 2 THEN parent_type ELSE NULL END AS parent_type_level_2, 
       CASE WHEN l = 2 THEN parent_id ELSE NULL END AS parent_id_level_2, 
       CASE WHEN l = 3 THEN id ELSE NULL END AS id_level_3, 
       CASE WHEN l = 3 THEN parent_type ELSE NULL END AS parent_type_level_3, 
       CASE WHEN l = 3 THEN parent_id ELSE NULL END AS parent_id_level_3, 
       CASE WHEN l = 4 THEN id ELSE NULL END AS id_level_4, 
       CASE WHEN l = 4 THEN parent_type ELSE NULL END AS parent_type_level_4, 
       CASE WHEN l = 4 THEN parent_id ELSE NULL END AS parent_id_level_4, 
       CASE WHEN l = 5 THEN id ELSE NULL END AS id_level_5, 
       CASE WHEN l = 5 THEN parent_type ELSE NULL END AS parent_type_level_5, 
       CASE WHEN l = 5 THEN parent_id ELSE NULL END AS parent_id_level_5 
     FROM (SELECT id, 
         parent_type, 
         parent_id, 
         LEVEL AS l 
       FROM dat 
       START WITH id = 71123 
       CONNECT BY PRIOR parent_id = id)); 

は結果 ID_LEVEL_1ですFSE | 67114 | 67114 | DST | 67111 | 67111 | | 0 | | | | | |

あなたの質問に記載されているように5段階でしか動作しないことを理解してください。