使用两个表创建视图

时间:2016-07-16 07:02:59

标签: sql oracle oracle10g oracle-sqldeveloper

  1. 我想创建一个视图(总贷款金额 - 总存款金额) 使用表 loan_b deposit_b 怎么可能?
  2. 使用loan_b表创建另一个视图,每个贷款的父母(20%)。 表格如下:
  3. 以下是我用于表格的查询:

    CREATE TABLE loan_b( cust_id VARCHAR2(10), loan_id VARCHAR2(10), loan_amount NUMBER(10) ); 
    
    INSERT INTO loan_b VALUES(100,'100A',28500);
    
    INSERT INTO loan_b VALUES(102,'100B',2500);
    
    CREATE TABLE deposit_b( cust_id VARCHAR2(10), deposit_amount NUMBER(10) ); 
    
    INSERT INTO deposit_b VALUES(99,51700); 
    
    INSERT INTO deposit_b VALUES(99,2462);
    
    INSERT INTO deposit_b VALUES(55,55200);
    

1 个答案:

答案 0 :(得分:0)

您可以轻松地创建一个视图作为选择,然后一旦您确定了您需要的选择,您可以创建您查看,例如:

create view your_view_name as 
select 
   a.cust_id cust_id
 , a.loan_id load_id
 , a.loan_amount loan_amount
 , b.deposit_amount deposit_amount
from loan_b a
inner join deposit_b on a.cust_id = b.cust_id;

或者如果您只需要总和

create view your_view_name2 as 
select 
   sum(a.loan_amount) sum_loan_amount
 , sum(b.deposit_amount)  sum_deposit_amount
from loan_b a
inner join deposit_b on a.cust_id = b.cust_id;

和diff你应该使用(我已经用原始表名更改了表别名)

create view your_view_name3 as 
select 
   loan_b.loan_amount - deposit_b.deposit_amount   diff_load_deposit_amount
from loan_b 
inner join deposit_b on loan_b.cust_id = deposit_b.cust_id;