Oracle中的父子关系查询

时间:2013-05-08 04:57:21

标签: oracle plsql

我有这样的表

表-A

Conversion_logic       Output_param

func(a,b)               c

func(d)                 e

func(c)                 d

func(e)                 f

这里的行描述了func(a,b)给了我“c”,现在这个“c”被用作func(c)并给我“d”,现在这个“d”被用作func(d)并给我“e”,现在这个“e”被用作func(e),它给了我“f”

所以我想要一个这样的输出

1) That row should be the first row whose output has no dependency.
2) From there it should follow parent child relation

Conversion_logic       Output_param
func(e)                f
func(d)                e
func(c)                d
func(a,b)              c

2 个答案:

答案 0 :(得分:2)

您可以使用分层查询。

SELECT     table_name.*
  FROM table_name
CONNECT BY PRIOR conversion_logic = 'func(' || output_param || ')'
START WITH conversion_logic = 'func(e)';

<强>更新

SELECT     table_name.*
  FROM table_name
CONNECT BY PRIOR conversion_logic = 'func(' || ooutput_param || ')'
START WITH ooutput_param =
          (SELECT a.ooutput_param
             FROM table_name a
            WHERE 'func(' || a.ooutput_param || ')' NOT IN (
                                                 SELECT b.conversion_logic
                                                   FROM table_name b));

不确定此查询的效果。也许有更好,更有效的。

答案 1 :(得分:0)

尝试

SELECT t1.*
  FROM taba t1
  LEFT OUTER JOIN taba t2 ON instr(replace(t1.Conversion_logic, 'func'), t2.Output_param) > 0
START WITH t2.Conversion_logic IS NULL
CONNECT BY instr(replace(t1.Conversion_logic, 'func'), PRIOR t1.Output_param) > 0
ORDER BY LEVEL DESC

Here is a sqlfiddle demo