oracle sql

jbose2ul  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(425)

我在一个名为cpgrprulrel的表中有以下数据。

CPGRPRULREL_NUM | CPGRPRULREL_NUM_PRIOR | STATUS     | CNDQUALSRC_CD | CNDQUALTYP_CD | CPGRP_NUM
--------------------------------------------------------------------------------------------------
41292           | 41289                 | Active     | PRODCNT       | PRODCNTUNQ    | 
41289           | 41286                 | Superseded | OTHERPG       |               | 118729
41286           | 41283                 | Superseded | SUBM          | VOLUME        | 
41283           | 41280                 | Superseded | OTHERPG       |               | 118729
41280           | 41277                 | Superseded | OTHERPG       |               | 118729
41277           | null                  | Superseded | PRODCNT       | PRODCNTALL    |

在这里,cpgrprulrel\u num\u prior是到cpgrprulrel\u num的链接(即具有外键约束-一种父子关系)。
我想用cpgrp\u num=118729删除所有。在此之前,我需要更新剩余行的cpgrprulrel\u num\u prior的值,以便正确的链接保持不变。
我需要更新查询,这将只更新行有cpgrp_num<>118729和以下数据后运行更新。

CPGRPRULREL_NUM | CPGRPRULREL_NUM_PRIOR | STATUS     | CNDQUALSRC_CD | CNDQUALTYP_CD | CPGRP_NUM
--------------------------------------------------------------------------------------------------
41292           | 41286                 | Active     | PRODCNT       | PRODCNTUNQ    | 
41289           | 41286                 | Superseded | OTHERPG       |               | 118729
41286           | 41277                 | Superseded | SUBM          | VOLUME        | 
41283           | 41280                 | Superseded | OTHERPG       |               | 118729
41280           | 41277                 | Superseded | OTHERPG       |               | 118729
41277           | null                  | Superseded | PRODCNT       | PRODCNTALL    |

请注意,有两行cpgrprulrel\u num 41292和41286的cpgrprulrel\u num\u先前已更新。

d6kp6zgx

d6kp6zgx1#

所以你想得到前一个 CPGRPRULREL_NUM 哪里 CPGRP_NUM <> 118729 ?
这样的方法应该有用:
构建树,从第一个开始(之前为空)
如果前一个 CPGRP_NUM 是118729,那么:
返回上一个 CPGRPRULREL_NUM_PRIOR 或者 CPGRPRULREL_NUM 如果为空
给:

with rws as (
  select 41292 num, 41289 num_prior, null cpgrp_num from dual union all
  select 41289 num, 41286 num_prior, 118729 from dual union all
  select 41286 num, 41283 num_prior, null from dual union all
  select 41283 num, 41280 num_prior, 118729 from dual union all
  select 41280 num, 41277 num_prior, 118729 from dual union all
  select 41277 num, null  num_prior, null from dual 
), tree ( num, num_prior, cpgrp_num ) as (
  select * from rws
  where  num_prior is null
  union all
  select r.num, 
         case
           when t.cpgrp_num = 118729 then nvl ( t.num_prior, t.num ) 
           else r.num_prior
         end num_prior, r.cpgrp_num 
  from   rws r
  join   tree t
  on     t.num = r.num_prior
)
  select * from tree;

NUM         NUM_PRIOR    CPGRP_NUM   
   41277       <null>       <null> 
   41280        41277       118729 
   41283        41277       118729 
   41286        41277       <null> 
   41289        41286       118729 
   41292        41286       <null>
xlpyo6sf

xlpyo6sf2#

使用分层子查询查找每个条目的叶值:

update cpgrprulrel c
  set CPGRPRULREL_NUM_PRIOR =
    (select CPGRPRULREL_NUM_PRIOR from cpgrprulrel
      where connect_by_isleaf = 1
      start with CPGRPRULREL_NUM = c.CPGRPRULREL_NUM_prior and cpgrp_num = 118729
      connect by prior CPGRPRULREL_NUM_prior = CPGRPRULREL_NUM and cpgrp_num = 118729)
  where cpgrp_num is null

D小提琴
然后可以删除不需要的行。

相关问题