【问题标题】:Updating the column values in one table based on values in other tables-Oracle SQL根据其他表中的值更新一个表中的列值-Oracle SQL
【发布时间】:2015-12-12 04:58:24
【问题描述】:

我的问题很关键,我有如下三个表格...

投资者表:

Investor_ID Total_amt_Invested Current_Value
ABC             100      

Investment_info 表:

Investor_ID    Entity_ID  No_of_units_bought
ABC             XYZ1       400
ABC             XYZ2       600
ABC             XYZ3      1000

Entity_info 表:

Entity_ID   Listed_NPV current_NPV
XYZ1        .05           1
XYZ2        .05           2
XYZ3        .05           3

在上表中,Investor 表中的 current_value 应自动计算为 400*1+600*2+3*1000.......如何更新列信息?有什么建议,请。

【问题讨论】:

  • 看起来很基本,你为什么不努力解决问题

标签: sql oracle multiple-columns


【解决方案1】:

自动编译列的一些奇特示例

-- create base tables
CREATE TABLE investor
  (investor_id        VARCHAR2(30),
   total_amt_invested INTEGER,
   current_value      INTEGER);

CREATE TABLE investment_info
  (investor_id        VARCHAR2(30),
   entity_id          VARCHAR2(30),
   no_of_units_bought INTEGER);

CREATE TABLE entity_info
  (entity_id   VARCHAR2(30),
   listed_npv  NUMBER,
   current_npv INTEGER);

-- insert data   
insert into investor values ('ABC', 100, null);
insert into investment_info values ('ABC', 'XYZ1', 400); 
insert into investment_info values ('ABC', 'XYZ2', 600); 
insert into investment_info values ('ABC', 'XYZ3', 1000); 
insert into entity_info values ('XYZ1', .05, 1); 
insert into entity_info values ('XYZ2', .05, 2); 
insert into entity_info values ('XYZ3', .05, 3); 
insert into investor values ('BCD', 100, null); 
insert into investment_info values ('BCD', 'XYZ4', 350); 
insert into entity_info values ('XYZ4', .05, 4);
commit;

-- create function to return total invest amount   
CREATE OR REPLACE FUNCTION get_total_invest(p_investor_id VARCHAR2)
RETURN NUMBER
DETERMINISTIC
RESULT_CACHE 
-- you don't need RELIES_ON if version 11.2+
RELIES_ON (investment_info, entity_info)
IS
  l_result   NUMBER;
BEGIN
  SELECT sum(no_of_units_bought * current_npv)
    INTO l_result
    FROM investment_info 
         JOIN entity_info
         ON investment_info.entity_id = entity_info.entity_id      
   WHERE investor_id = p_investor_id;
   RETURN l_result;
END;
/

-- alter table to add autocalculated virtual column
ALTER TABLE investor ADD (auto_current_value GENERATED ALWAYS AS (get_total_invest(investor_id)));

-- recreate function without DETERMINISTIC (read documantion about virtual column)
CREATE OR REPLACE FUNCTION get_total_invest(p_investor_id VARCHAR2)
RETURN NUMBER
RESULT_CACHE
-- you don't need RELIES_ON if version 11.2+
RELIES_ON (investment_info, entity_info)
IS
  l_result   NUMBER;
BEGIN
  SELECT sum(no_of_units_bought * current_npv)
    INTO l_result
    FROM investment_info 
         JOIN entity_info
         ON investment_info.entity_id = entity_info.entity_id      
   WHERE investor_id = p_investor_id;
  RETURN l_result;
END;
/

SELECT *
  FROM investor;

insert into investment_info values ('ABC', 'XYZ3', 1100); 
COMMIT;

SELECT *
  FROM investor;

【讨论】:

    【解决方案2】:

    这将为每个Investor_ID 计算Current Value

    select inv.Investor_ID ,
           sum(inv_info.No_of_units_bought*ent_info.current_NPV) as Current_Value    
    from Investor inv 
    inner join Investment_info inv_info on inv.Investor_ID=inv_info.Investor_ID 
    inner join Entity_info ent_info on inv_info.Entity_ID=ent_info.Entity_ID
    group by inv.Investor_ID 
    

    【讨论】:

      【解决方案3】:

      这是您要查找的更新声明:

      update investor inv set current_value = (select
      sum(i.no_of_units_bought * e.current_npv) 
      from investment_info i join entity_info e
      on i.entity_id = e.entity_id
      where inv.investor_id = i.investor_id
      group by i.investor_id);
      

      以下是遵循的步骤:

      • 创建表
      create table investor (investor_id vachar2(30), total_amt_invested integer, current_value integer);
      create table investment_info (investor_id varchar2(30), entity_id varchar2(30), no_of_units_bought integer);
      create table entity_info (entity_id varchar2(30), listed_npv number, current_npv integer);
      
      • 插入数据
      insert into investor values ('ABC', 100, null);
      insert into investment_info values ('ABC', 'XYZ1', 400); 
      insert into investment_info values ('ABC', 'XYZ2', 600); 
      insert into investment_info values ('ABC', 'XYZ3', 1000); 
      insert into entity_info values ('XYZ1', .05, 1); 
      insert into entity_info values ('XYZ2', .05, 2); 
      insert into entity_info values ('XYZ3', .05, 3); 
      insert into investor values ('BCD', 100, null); 
      insert into investment_info values ('BCD', 'XYZ4', 350); 
      insert into entity_info values ('XYZ4', .05, 4);
      
      • 运行上述更新语句以更新投资者并验证
      SQL> select * from investor;
      
      INVESTOR_ID              TOTAL_AMT_INVESTED CURRENT_VALUE
      ------------------------------ ------------------ -------------
      ABC                         100      4600
      BCD                         100      1400
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-28
        • 1970-01-01
        • 2018-12-04
        • 2021-07-16
        • 2011-04-19
        • 1970-01-01
        相关资源
        最近更新 更多