我有表Parent_tbl,它由3列H_N,Col58和Type组成,前两列将具有相同的值,只有列类型不同。
我有一个子表,其中col58定义了与父项的关系,但是child_tbl中的其余列仅特定于该表,H_N是两个表中的唯一列。
我需要在PARENT_TBL中将TYPE更新为EXCHANGE,当我发现CHILD_TBL I_STATUS具有S,R和V等所有值时,否则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');
答案 0 :(得分:1)
使用此
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
这会在过滤器col58
之后为count(distinct(i_status))=3
提供i_status in ('S','R','V')
。因此,只有3
的每个状态至少为1时才会'S','R','V'
。现在在exists
子句中使用它并添加
上述查询and c.col58=p.col58
中的where条件,以便在更新时将其与parent
表连接。
请首先尝试测试数据并尝试此操作而不提交原始数据。只有当您确定获得预期结果时才提交。
答案 1 :(得分:0)
使用适当的分组查找子表(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'