使用同一表中其他列的计算mede更新表中的列

时间:2017-05-09 16:54:35

标签: sql stored-procedures

我有一种情况需要更新临时表中的行,并对来自同一个表的其他一些列数据进行计算。

我能够为分裂和总和做这个,但不是像下面那样复杂的 我应该做的是

 UPDATE t
 SET    t.Value_Numeric = ((t.row9-((1-t.row28)*t.row26))/((1-t.row91))/(t.row33)-t.row85)
 FROM   #tempTable t
 where t.order = 93

 ** t.row9 means t.Value_Numeric  where t.order = 9

我能够像这样进行分组

UPDATE #tempTable
 SET    #tempTable.Value_Numeric = reim.cmi
 FROM   #tempTable t
        INNER JOIN ( SELECT t1.Value_Numeric AS IPAllw ,
                            t2.Value_Numeric AS IPElig ,
                            t1.Value_Numeric / NULLIF(t2.Value_Numeric,0) AS cmi ,
                            t1.WorkingID
                     FROM   #tempTable t1
                            JOIN #tempTable t2 ON t1.WorkingID = t2.WorkingID
                                                  AND t1.order = 78
                                                  AND t2.order = 74
                     GROUP BY t1.WorkingID ,
                            t1.Value_Numeric ,
                            t2.Value_Numeric
                   ) reim ON reim.WorkingID = t.WorkingID
 WHERE  t.order = 79

帮我弄清楚更新复杂计算的查询。

由于

2 个答案:

答案 0 :(得分:0)

我在答案中假设Oracle基于给出问题的plsql标签。

一种可能的解决方案是创建一个辅助函数,如下所示:

create or replace function get_temptable_value(p_row in number) return temptable.value_numeric%type
is
  pragma AUTONOMOUS_TRANSACTION; 
  v_val  temptable.value_numeric%type;
begin
  begin
    select value_numeric 
    into v_val
    from temptable 
    where row_order = p_row;
  exception 
    when no_data_found then
      v_val := null;
  end;

  return v_val;      
end;  

如果找不到给定的行,则该函数返回null,但根据您的要求返回0可能更好。自治pragma允许从更新语句调用它(否则给出ORA-04091):

update temptable t
SET    t.Value_Numeric = ((get_temptable_value(9)-((1-get_temptable_value(28))* get_temptable_value(26)))/((1- get_temptable_value(91)))/( get_temptable_value(33))- get_temptable_value(85))
where t.row_order = 93;

<强> 2。另一个解决方案是使用模型cluase:

create or replace view temptable_upd
as  
select *
from temptable t
model dimension by (row_order) 
      measures (value_numeric, value_numeric as new_value_numeric)
      rules (new_value_numeric[93] = ((value_numeric[9]-((1-value_numeric[28])* value_numeric[26]))/((1- value_numeric[91]))/( value_numeric[33])- value_numeric[85])
            );

在规则部分,您可以定义任何行间计算。

使用该视图,您可以更新表格:

update temptable t
set t.value_numeric = (select new_value_numeric from temptable_upd tupd where tupd.row_order = t.row_order) 
where t.row_order = 93;

答案 1 :(得分:0)

  

Create or replace procedure abc Is V_count number; Begin Select ur_calculations o/p into v_count from table ;Update Ur_table set col1 = v_count Where col2 =value;

提交; END;