Oracle-层次结构表中的End Child节点

时间:2019-01-31 15:35:05

标签: oracle hive impala

我正尝试从具有parent_node_id和child_node_id的层次结构表中查找最低级别的子节点,如下所示。 它还返回了中级子节点。请帮助修改此查询并获得所需的结果。

请建议将同一查询转换为Impala,当我在HUE中运行此查询时,它显示错误-找不到tableR参考。

WITH tableR (parent_node_id, child_node_id)
             AS (SELECT e.parent_node_id, e.child_node_id
                   FROM hierarchy e
                  WHERE     parent_node_id IN (select parent_node_id from hierarchy)
                 UNION ALL
                 SELECT e.parent_node_id, e.child_node_id
                   FROM hierarchy e 
                        INNER JOIN tableR d
                           ON e.parent_node_id = d.child_node_id
                  )
        SELECT parent_node_id, child_node_id
          FROM tableR

源表:

+-------------+--------------+-------------+
    |child_node_id|parent_node_id| child_desc  |
    +-------------+--------------+-------------+
    |     C1      |      P1      |     Private |
    +-------------+--------------+-------------+
    |     C2      |      P2      |     Public  |     
    +-------------+--------------+-------------+
    |     C11     |      C1      |     Party   |
    +-------------+--------------+-------------+
    |     C12     |      C11     |     Equity  |
    +-------------+--------------+-------------+
    |     123     |      C12     |   Placement |
    +-------------+--------------+-------------+

预期输出:

+-------------+--------------+-------------+
|parent_node  |lowest_l_child| child_desc  |
+-------------+--------------+-------------+
|     P1      |      123     |  Placement  |
+-------------+--------------+-------------+
|      P2     |       C2    |    Public    |
+-------------+--------------+-------------+
|     C1      |      123     |  Placement  |
+-------------+--------------+-------------+
|     C11     |      123     |   Placement |
+-------------+--------------+-------------+
|     C12     |    123       |   Placement |
+-------------+--------------+-------------+

1 个答案:

答案 0 :(得分:0)

带有过滤叶的标准connect by查询可提供预期的输出:

select connect_by_root(parent_node_id) root_parent, child_node_id, child_desc
  from hierarchy h
  where connect_by_isleaf = 1
  start with parent_node_id in (select parent_node_id from hierarchy)
  connect by prior child_node_id = parent_node_id

dbfiddle demo

很不幸,我不知道Impala的解决方案:(