2014-04-14 63 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

我编辑了答案。检查它,如果它的工作通过单击刻度符号来接受答案。 –