2014-04-14 64 views
0

請幫助。如何完成以下操作:使用同一表中的select查詢-2更新select select-1

該表包含日常交易數據。我們的目標是使用昨天的記錄的計算值(那3列)來更新/插入當前日期的當日記錄中的3列中的值。我還有最後40天更新基於:我的代碼

trunc(sysdate)-39 = calculated value of trunc(sysdate)-40 
    trunc(sysdate)-38 = calculated value of trunc(sysdate)-39 
    trunc(sysdate)-37 = calculated value of trunc(sysdate)-36 
    . 
    . 
    . 
    . 
    trunc(sysdate)= calculated value of trunc(sysdate)-1. 

例如:

marge into 

(select trans_date, store, item, reason, col1, col2, col3 
from tb1 where tb1.trans_date = trunc(sysdate)) today 

using 

(select trans_date, store, item, reason, col1, col2, col3 
from tb1 
where tb1.trans_date = trunc(sysdate-1)) yesterday 

when matched then 
update set 
(today.col1 = yesterday.col1 + 1 
today.col2 = decode(yesterday.reason,today.reason,today.col2+1,1) 
today.col3 = yesterday.trans_date) 

WHEN NOT MATCHED THEN 
INSERT (today.col1, today.col2, today.col3) 
VALUES (
     1, 1, 
     (select max(trans_date) from tb1 
      where tb1.trans_date < trunc(sysdate)-1) 
      and tb1.store=today.store 
      and tb1.item=today.item); 

請注意:每天記錄可能有重複如下。

今天:

trans_date store item reason   col1 col2 col3 ***(expected values)*** 

    14/04/14 999 100 'short supply'  -  -  - ==> 2,2,13/04/14 
    14/04/14 999 100 'short supply'  -  -  - ==> 2,2,13/04/14 
    14/04/14 998 101 'Damaged'   -  -  - ==> 2,2,11/04/14 
    14/04/14 990 105 'Returned'   -  -  - ==> 2,1,13/04/14 
    14/04/14 995 107 'Returned'   -  -  - ==> 1,1,14/04/14 

昨天:

trans_date store item reason   col1 col2 col3 

    13/04/14 999 100 'short supply' 1 1 13/04/14 
    13/04/14 999 100 'short supply' 1 1 13/04/14 
    13/04/14 998 101 'Damaged'   1 1 11/04/14 
    13/04/14 990 105 'Transferred'  1 1 13/04/14 
+0

如果它可以幫助你:每天將有約45萬條記錄被更新/插入。更新日常記錄可以通過存儲過程完成。但挑戰是一次更新歷史。 – user3531676

回答

0

如果你需要經常創建一個存儲過程。這將有助於

create or replace procedure SP_TEST 
as 
// declare your variables // 
cursor c is select trans_date, store, item, reason, col1, col2, col3 
    from tb1 where tb1.trans_date = trunc(sysdate-1); 
begin 

    for rec in c loop 
     // do your calculations // 
    select count(*) into v_v1 from tb1 where tb1.trans_date = trunc(sysdate) 
    if v_v1=0 then 
     // do insert 
    else 
     //do update 
    end if; 
    end loop; 
    exception 
     // exception part 
    end; 

如果你不想重複在你的光標查詢中使用DISTINCT或使用限制在表

對於歷史數據的使用是這樣的

create or replace procedure SP_TEST(P_DATE DATE) 
as 
// declare your variables // 
cursor c is select trans_date, store, item, reason, col1, col2, col3 
    from tb1 where tb1.trans_date = trunc(P_DATE-1); 
begin 

    for rec in c loop 
     // do your calculations // 
    select count(*) into v_v1 from tb1 where tb1.trans_date = trunc(P_DATE) 
    if v_v1=0 then 
     // do insert 
    else 
     //do update 
    end if; 
    end loop; 
    exception 
     // exception part 
    end; 
+0

謝謝Rock'em。我會試一試。只是想知道如何更新歷史數據... – user3531676

+0

將參數傳遞給過程說P_DATE這將是您處理的日期。並在遊標查詢條件中使用P_DATE-1。所以您從P_DATE的前一天獲取數據,對其進行處理並存儲到P_DATE。所以你可以給任何日期和處理歷史數據。 –

+0

我編輯了答案。檢查它,如果它的工作通過單擊刻度符號來接受答案。 –