2017-05-29 47 views
1

我有表Parent_tbl,它由3列H_N,Col58和Type組成,前兩列將具有相同的值,只有列類型不同。考慮到oracle中子表的值,更新父表

我有一個子表,其中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

給出預期的輸出以及給出的2個表的數據。 – Utsav

+0

Utsav,我添加了預期的輸出 – JDev

回答

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 
) 

精通lanation:

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子句中使用此項並添加 a,其中條件在上述查詢中and c.col58=p.col58在更新時與parent表一起加入。

請首先嚐試這個測試數據,並嘗試這個,而不提交原始數據。只有當你確定你有預期的結果時才提交。

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'