使用两个表创建视图
Create View using two tables
- 我要创建一个视图(贷款总额-存款总额)
使用 table loan_b 和 deposit_b 怎么可能?
- 并使用 loan_b table 创建另一个视图,每笔贷款的亲子关系 (20%)。
table 如下:
以下是我用于 table 的查询:
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);
您可以轻松地创建一个视图 select 然后一旦您确定了您需要的 select 您就可以创建您的视图例如:
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 你应该使用(我已经用原来的 tablename 改变了 table 别名)
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;
- 我要创建一个视图(贷款总额-存款总额) 使用 table loan_b 和 deposit_b 怎么可能?
- 并使用 loan_b table 创建另一个视图,每笔贷款的亲子关系 (20%)。 table 如下:
以下是我用于 table 的查询:
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);
您可以轻松地创建一个视图 select 然后一旦您确定了您需要的 select 您就可以创建您的视图例如:
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 你应该使用(我已经用原来的 tablename 改变了 table 别名)
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;