考虑到 oracle 中 child table 的值更新 parent table

Updating the parent table considering the values of the child table in oracle

我有 table Parent_tbl,它由 3 列组成 H_N,Col58 和 Type 这两个前两列将具有相同的值,只有列类型不同。

我有一个 child table,其中 col58 定义了与 parent 的关系,但 child_tbl 中的其余列特定于 table只有 H_N 是两个 table 中的唯一列。

每当我发现 CHILD_TBL I_STATUS 具有 S、R 和 V 等所有值时,我需要将 PARENT_TBL 中的 TYPE 更新为 EXCHANGE,否则 parent_tbl 类型保持不变,我们该怎么做?

Parent_tbl.col58 = 1140 类型应该是 'EXCHANGE' 因为 child_tbl.col58 = 1140 有每个字母,即 S,R,V。

这是示例的 DDL。

CREATE TABLE PARENT_TBL (
    H_N number, 
    col58 number,
    TYPE varchar(100)
);
Insert into PARENT_TBL (H_N,COL58,TYPE) values (2,2,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (16,16,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (20,20,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (34,34,'VOID');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (38,38,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (102,102,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (111,111,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (117,117,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (1140,1140,'RETURN');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (131,131,'SALE');

commit;

CREATE TABLE CHILD_TBL
(
    I_STATUS varchar(100),
    H_n number,
    col58 number
);


Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('S',3,2);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('S',5,2);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('S',7,2);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('S',8,2);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('S',10,2);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('S',1141,1140);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('V',1142,1140);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('R',1143,1140);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('R',1144,1140);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('S',1145,1140);

commit;

预期输出:

  truncate table PARENT_TBL ;
     Insert into PARENT_TBL (H_N,COL58,TYPE) values (2,2,'SALE');
        Insert into PARENT_TBL (H_N,COL58,TYPE) values (16,16,'SALE');
        Insert into PARENT_TBL (H_N,COL58,TYPE) values (20,20,'SALE');
        Insert into PARENT_TBL (H_N,COL58,TYPE) values (34,34,'VOID');
        Insert into PARENT_TBL (H_N,COL58,TYPE) values (38,38,'SALE');
        Insert into PARENT_TBL (H_N,COL58,TYPE) values (102,102,'SALE');
        Insert into PARENT_TBL (H_N,COL58,TYPE) values (111,111,'SALE');
        Insert into PARENT_TBL (H_N,COL58,TYPE) values (117,117,'SALE');
        Insert into PARENT_TBL (H_N,COL58,TYPE) values (1140,1140,**'EXCHANGE'**);
        Insert into PARENT_TBL (H_N,COL58,TYPE) values (131,131,'SALE');

在子 table (CHILD_TBL) 中查找具有适当分组的行并使用 merge:

merge into parent_tbl p
using (select col58 
         from child_tbl
         group by col58
         having count(decode(i_status, 'S', 1)) > 0 
            and count(decode(i_status, 'R', 1)) > 0
            and count(decode(i_status, 'V', 1)) > 0) c
on (p.col58 = c.col58)
when matched then update set type = 'EXCHANGE'

使用这个

update PARENT_TBL p
set TYPE='EXCHANGE'
where exists 
( select 1
    from child_tbl c
    where
    i_status in ('S','R','V')
    and c.col58=p.col58
     group by col58
     having count(distinct(i_status))=3
)

解释:

select col58 
from child_tbl c
where
  i_status in ('S','R','V')
  group by col58
  having count(distinct(i_status))=3

这将在过滤器 i_status in ('S','R','V') 之后为您提供 col58,其中 count(distinct(i_status))=3。因此只有当 'S','R','V' 的每个状态至少有 1 个时,它才会是 3。现在在 exists 子句中使用它并添加 上述查询中的 where 条件 and c.col58=p.col58 在更新时将其与 parent table 连接。

请先对您的测试数据进行尝试,并在不提交原始数据的情况下进行尝试。只有当你确定你得到了预期的结果时才提交。