我的问题很关键,我有三张桌子如下......
投资者表:
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 .......如何更新列信息?请提出任何建议。
答案 0 :(得分: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;
答案 1 :(得分:0)
这将为每个Investor_ID
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
"// Primary Controls"
答案 2 :(得分:0)
以下是您要查找的更新声明:
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