【问题标题】:updating select query-1 using select query-2 in the same table在同一个表中使用 select query-2 更新 select query-1
【发布时间】:2014-05-28 10:24:27
【问题描述】:

请帮忙。我该如何完成以下操作:

该表包含每日交易数据。目的是使用昨天的记录的计算值(这 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

【问题讨论】:

  • 如果有帮助:每天将有大约 450000 条记录需要更新/插入。可以通过存储过程更新日常记录。但挑战在于一次性更新历史记录。

标签: sql oracle merge insert-update upsert


【解决方案1】:

如果您需要它,通常创建一个存储过程。这会有所帮助

 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;

【讨论】:

  • 感谢 Rock'em。我会试一试。只是想知道如何更新历史数据...
  • 将参数传递给过程说 P_DATE 这将是您处理的日期。并且在游标查询条件中使用 P_DATE-1。所以你从 P_DATE 的前一天获取数据,处理它并为 P_DATE 存储。这样您就可以返回任何日期并处理历史数据。
  • 我已经编辑了答案。检查它,如果它的工作通过单击勾号接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多