【问题标题】:Oracle Update statementOracle 更新声明
【发布时间】:2015-04-12 16:05:34
【问题描述】:

我正在尝试更新一个有点棘手的表。让我试着解释一下。。 例如这里是表格数据

这是我想要更新 col5 的方式。 对于每条记录,检查 col3 是否有值,如果是,则检查该值是否在 col2 中。如果两个条件都满足,则获取 col1 的最大值,因为 col2 将有重复项。 如果 col3 没有值,我们需要检查 col4 并与 col2 比较并得到 col1。

例如:对于第 5 行,我将首先检查 col3,它的值为“b”,我将检查 col2 的值“b”,我有 3 个匹配项(col1 - id 2,7,9)将需要从 col1 中取 9 并在第 5 行更新 col5。

请提出您宝贵的想法/想法..

【问题讨论】:

    标签: oracle11g


    【解决方案1】:

    我已经为您的第一个条件编码,您匹配 col3(对于现有和不存在的条件),您可以类似地在过程中编写代码以匹配 col4。

    create or replace procedure proc_cal is
      d_col1 varchar2(3);
      d_col2 varchar2(3);
      maxcol varchar2(3):= '0';
    begin
      for i in (select * from coldata) loop
      dbms_output.put_line('in here '|| i.col3);
        if (i.col3 is not null) then
          dbms_output.put_line('value exists for col3 and is ' || i.col3);
          for j in (select * from coldata) loop
    
            if (j.col2 = i.col3) then
              dbms_output.put_line('value exists for col2 and col1 value is ' ||
                                   j.col1);
             dbms_output.put_line('the prev maxcol is '|| maxcol);
              if (maxcol < j.col1) then
              dbms_output.put_line('checking'); 
               maxcol := j.col1;
    
                dbms_output.put_line('current maxcol is  ' || maxcol);
    
              end if;
            end if;
    
          end loop;
          update coldata set col5 = maxcol where col3=i.col3;
            dbms_output.put_line('max value update and is ' || maxcol);
            maxcol:=0;
    
        else
          update coldata set col5 = null where col3=i.col3;
          end if;
      end loop;
    end;
    
    
    
    SCOTT@research 13-APR-15> select * from coldata;
    
    COL COL COL COL COL
    --- --- --- --- ---
    1   a       a
    2   b       c
    3   a   a       8
    4   c       c
    5   a   b       9
    6   c       b
    7   b   c   e   10
    8   a       a
    9   b       b
    10  c       c
    

    【讨论】:

      【解决方案2】:

      如果您将来可以为您的示例提供 CREATE DDL 和一些 INSERT 语句,那就太棒了,但这里是一个非程序解决方案。

      CREATE TABLE t (
             col1 NUMBER,
             col2 VARCHAR2(1),
             col3 VARCHAR2(1),
             col4 VARCHAR2(1),
             col5 NUMBER
      );
      
      INSERT INTO t VALUES (1, 'a', NULL, 'a', NULL);
      INSERT INTO t VALUES (2, 'b', NULL, 'c', NULL);
      INSERT INTO t VALUES (3, 'a', 'a', NULL, NULL);
      INSERT INTO t VALUES (4, 'c', NULL, 'c', NULL);
      INSERT INTO t VALUES (5, 'a', 'b', NULL, NULL);
      INSERT INTO t VALUES (6, 'c', NULL, 'b', NULL);
      INSERT INTO t VALUES (7, 'b', 'c', 'e', NULL);
      INSERT INTO t VALUES (8, 'a', NULL, 'a', NULL);
      INSERT INTO t VALUES (9, 'b', NULL, 'b', NULL);
      INSERT INTO t VALUES (10, 'c', NULL, 'c', NULL);
      
      UPDATE t t1
         SET t1.col5 =
             (SELECT MAX(t2.col1)
                FROM t t2
               WHERE t2.col2 = t1.col3)
       WHERE t1.col3 IS NOT NULL;
      
      SELECT *
        FROM t;
      /*
      col col col col col
      1   a       a 
      2   b       c 
      3   a   a       8
      4   c       c 
      5   a   b       9
      6   c       b 
      7   b   c   e   10
      8   a       a 
      9   b       b 
      10  c       c 
      */
      

      【讨论】:

        猜你喜欢
        • 2017-01-26
        • 1970-01-01
        • 1970-01-01
        • 2013-01-25
        • 2021-04-21
        • 1970-01-01
        • 2022-01-22
        • 1970-01-01
        • 2014-02-20
        相关资源
        最近更新 更多