对Oracle中具有不同ID的多个记录重复列值

时间:2019-05-22 09:13:48

标签: sql oracle oracle12c

我正在使用Oracle 12c。

我有一个分层的Oracle表,我想在其中使用父节点的简称(即start with parent_node_is is null) 属于该父级的所有子级节点。

例如:表名称:nodes_tab

NODE_ID    SHORT_NAME     PARENT_NODE_ID
---------- -------------- --------------
1          Parent Node-1  NULL
2          Child Node-2   1
3          Child Node-3   1
4          Child Node-4   2
5          Child Node-5   2
6          Child Node-6   4
7          Child Node-7   6

我想要实现的是在上面的nodes_tab中查询所有node_id,但是分配属于父节点的short_name。

理想情况下,我只想对其余的node_id从2到7重复使用Parent Node-1相同的名称,但是不确定SQL查询应该是什么。我看了看LAG,但似乎无法解决问题。

我追求的结果是:

NODE_ID    SHORT_NAME   
---------- -------------
1          Parent Node-1
2          Parent Node-1
3          Parent Node-1
4          Parent Node-1
5          Parent Node-1
6          Parent Node-1
7          Parent Node-1

2 个答案:

答案 0 :(得分:5)

从层次上讲,您的数据如下所示:

SQL> with nodes_tab (node_id, short_name, parent_node_id) as
  2    (select 1, 'Parent Node-1', null from dual union all
  3     select 2, 'Child Node-2' , 1    from dual union all
  4     select 3, 'Child Node-3' , 1    from dual union all
  5     select 4, 'Child Node-4' , 2    from dual union all
  6     select 5, 'Child Node-5' , 2    from dual union all
  7     select 6, 'Child Node-6' , 4    from dual union all
  8     select 7, 'Child Node-7' , 6    from dual
  9    )
 10  select node_id,
 11         lpad(' ', 2 * level) || short_name as short_name,
 12         parent_node_id,
 13         connect_by_root short_name as root_node
 14  from nodes_tab
 15  start with parent_node_id is null
 16  connect by prior node_id = parent_node_id;

   NODE_ID SHORT_NAME                PARENT_NODE_ID ROOT_NODE
---------- ------------------------- -------------- -------------
         1   Parent Node-1                          Parent Node-1
         2     Child Node-2                       1 Parent Node-1
         4       Child Node-4                     2 Parent Node-1
         6         Child Node-6                   4 Parent Node-1
         7           Child Node-7                 6 Parent Node-1
         5       Child Node-5                     2 Parent Node-1
         3     Child Node-3                       1 Parent Node-1

7 rows selected.

SQL>

请注意ROOT_NODE,它是通过使用CONNECT_BY_ROOT获取的-似乎您希望所有SHORT_NAME都具有该值。

因此:如果我们删除缩进并应用上面已经看到的内容以及适当的ORDER BY子句,则最终结果为

SQL> with nodes_tab (node_id, short_name, parent_node_id) as
  2    (select 1, 'Parent Node-1', null from dual union all
  3     select 2, 'Child Node-2' , 1    from dual union all
  4     select 3, 'Child Node-3' , 1    from dual union all
  5     select 4, 'Child Node-4' , 2    from dual union all
  6     select 5, 'Child Node-5' , 2    from dual union all
  7     select 6, 'Child Node-6' , 4    from dual union all
  8     select 7, 'Child Node-7' , 6    from dual
  9    )
 10  select node_id,
 11         connect_by_root short_name as short_name
 12  from nodes_tab
 13  start with parent_node_id is null
 14  connect by prior node_id = parent_node_id
 15  order by node_id;

   NODE_ID SHORT_NAME
---------- -------------------------
         1 Parent Node-1
         2 Parent Node-1
         3 Parent Node-1
         4 Parent Node-1
         5 Parent Node-1
         6 Parent Node-1
         7 Parent Node-1

7 rows selected.

SQL>

答案 1 :(得分:1)

Oracle 11GR2及更高版本支持递归CTE(它们是标准SQL的一部分)。

这可行:

with cte (node_id, parent_id, short_name, lev) as (
      select node_id, coalesce(parent_node_id, node_id), short_name, 1
      from nodes_tab
      union all
      select cte.node_id, nt.parent_node_id, nt.short_name, lev + 1
      from cte join
           nodes_tab nt
           on cte.parent_id = nt.node_id 
     )
select *
from (select cte.*, row_number() over (partition by node_id order by lev desc) as seqnum
      from cte
     ) cte
where seqnum = 1;

Here是db <>小提琴。