How to extract data from three tables using SQL object relational statement in oracle 11g

泄露秘密 提交于 2019-12-14 03:01:01

问题


I have three tables Branch, Account_table and customer. I am trying to write a SQL statement for:

At each branch find customers who have the highest balance in their savings account. Displaying their names, the balance, the branch ID and the free overdraft limit in their current accounts.

I have created three tables and insert data:

Branch Table

BID        BADDRESS.STREET      BADDRESS.CITY        BADDRESS.P
---------- -------------------- -------------------- ----------
901        Nicholson Street     Edinburgh            EH11 5AB  
906        East End Garden      Glasgow              G181QP    
912        Fredrick Street      London               LA112AS   
918        Zink Terrace         Edinburgh            EH149UU 

Account_table

ACCNUM ACCTYPE            BALANCE BID.BID        INRATE LIMITOFFREEOD OPENDATE
------- --------------- ---------- ---------- ---------- ------------- --------
 1001 current              820.5 901              .005           800 01-MAY-11
 1010 saving                2155 906               .02             0 08-MAR-10
 1002 current               2600 912              .005          1000 10-APR-13
 1011 saving                4140 918               .02             0 24-OCT-13

Customer Table

CUSTID CADDRESS.STREET      CADDRESS.CITY  CADDRESS.POSTCODE    CNAME.FIRSTNAME CNAME.SURNAME       
---------- -------------------- ----------- -------------------- --------------- -----------
      1002 Adam Street          Edinburgh   EH112LQ              Jack            Smith               
      1003 Adam Street          Edinburgh   EH112LQ              Anna            Smith               
      1004 New Tweed            Edinburgh   EH1158L              Liam            Bain                
      1005 Dundas Street        Edinburgh   EH119MN              Usman           Afaque              
      1006 St Andres Square     Edinburgh   EH12LNM              Claire          Mackintosh  

Branch(bID, street, city, p_code, bPhone)

Account(accNum, accType, balance, bID, inRate, limitOfFreeOD, openDate)

Customer(custID, street, city, postCode, title, firstName, surName, custHomePhone,custMobile1, custMobile2, niNum)

Bold is primary key Italic is foreign key (In object relational we don't use Join if I am right).

This what I am trying to do but failed

select c.custid,
(select max(balance) from account_table a
where  c.CUSTID = a.bid.bid 
and a.acctype='saving' )as highest_saving,
c.cname.firstname,c.CNAME.surname
from  customer c;

Any help? Thanks.


回答1:


You are missing custID column in account table. I have added a few more rows of data to create the test case for your requirement.

drop table acct;
drop table branch;
drop table customer;

create table branch(bid number primary key, addr_street varchar2(100), addr_city varchar2(100), addr_p  varchar2(20));
insert into branch values(901,'Nicholson Street','Edinburgh','EH11 5AB');
insert into branch values(906,'East End Garden','Glasgow','G181QP');
insert into branch values(912,'Fredrick Street','London','LA112AS');
insert into branch values(918,'Zink Terrace','Edinburgh','EH149UU');
commit;

select * from branch;

Output:

BID ADDR_STREET         ADDR_CITY   ADDR_P
901 Nicholson Street    Edinburgh   EH11 5AB
906 East End Garden     Glasgow     G181QP
912 Fredrick Street     London      LA112AS
918 Zink Terrace        Edinburgh   EH149UU

create table customer(custid number primary key, caddr_street varchar2(100), caddr_city varchar2(100), 
                      caddr_p varchar2(10), fname varchar2(100), lname varchar2(100));
insert into customer values(1002,'Adam Street','Edinburgh','EH112LQ','Jack','Smith');
insert into customer values(1003,'Adam Street','Edinburgh','EH112LQ','Anna','Smith');
insert into customer values(1004,'New Tweed','Edinburgh','EH1158L','Liam','Bain');
insert into customer values(1005,'Dundas Street','Edinburgh','EH119MN','Usman','Afaque');
insert into customer values(1006,'St Andres Square','Edinburgh','EH12LNM','Claire','Mackintosh');
commit;

select * from customer;

Output:

CUSTID  CADDR_STREET        CADDR_CITY  CADDR_P FNAME   LNAME
1002    Adam Street         Edinburgh   EH112LQ Jack    Smith
1003    Adam Street         Edinburgh   EH112LQ Anna    Smith
1004    New Tweed           Edinburgh   EH1158L Liam    Bain
1005    Dundas Street       Edinburgh   EH119MN Usman   Afaque
1006    St Andres Square    Edinburgh   EH12LNM Claire  Mackintosh

create table acct(accnum number primary key, acctype varchar2(20), balance number, bid number
                  constraint acct_fk1 references branch(bid), 
                   inrate number, LIMITOFFREEOD number, OPENDATE date, custid number
                   constraint acct_fk2 references customer(custid));
insert into acct values(1001,'current',820.5,901,0.005,800,to_date('01-MAY-11','dd-mon-yy'),1002);
insert into acct values(1010,'saving',2155,906,0.02,0,to_date('08-MAR-10','dd-mon-yy'),1002);
insert into acct values(1002,'current',2600,912,0.005,1000,to_date('10-APR-13','dd-mon-yy'),1006);
insert into acct values(1011,'saving',4140,918,0.02,0,to_date('24-OCT-13','dd-mon-yy'),1004);
insert into acct values(1012,'saving',4155,906,0.02,0,to_date('08-MAR-10','dd-mon-yy'),1004);
insert into acct values(1013,'current',2600,918,0.005,1000,to_date('10-APR-13','dd-mon-yy'),1004);
commit;

select * from acct;

Output:

ACCNUM  ACCTYPE BALANCE BID INRATE  LIMITOFFREEOD   OPENDATE    CUSTID
1001    current 820.5   901 .005    800             01-MAY-11   1002
1010    saving  2155    906 .02     0               08-MAR-10   1002
1002    current 2600    912 .005    1000            10-APR-13   1006
1011    saving  4140    918 .02     0               24-OCT-13   1004
1012    saving  4155    906 .02     0               08-MAR-10   1004
1013    current 2600    918 .005    1000            10-APR-13   1004

select y.fname, y.lname, y.balance, y.bid,ac.accnum,ac.acctype,ac.LIMITOFFREEOD 
  from (select * 
          from (select b.bid, c.custid, a.accnum,a.balance, 
                       row_number() over(partition by b.bid order by a.balance desc) rn,
                       c.fname, c.lname
                  from acct a 
                       inner join 
                       branch b 
                    on a.bid = b.bid 
                       inner join 
                       customer c 
                    on a.custid = c.custid
                 where a.acctype = 'saving') x
         where x.rn = 1) y
       left join 
       acct ac 
    on y.custid = ac.custid 
   and y.bid = ac.bid 
   and ac.acctype = 'current';

Output:

FNAME   LNAME   BALANCE BID ACCNUM  ACCTYPE LIMITOFFREEOD
Liam    Bain    4140    918 1013    current 1000
Liam    Bain    4155    906 NULL    NULL    NULL


来源:https://stackoverflow.com/questions/49266565/how-to-extract-data-from-three-tables-using-sql-object-relational-statement-in-o

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!