sql >> Database teknologi >  >> RDS >> Oracle

Sådan udtrækkes data fra tre tabeller ved hjælp af SQL-objektrelationssætning i oracle 11g

Du mangler kunde-id-kolonnen i kontotabellen. Jeg har tilføjet nogle flere rækker med data for at oprette testcasen til dit krav.

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



  1. Sådan gendannes en database med Backup Manager

  2. MySQL:Kan en lagret procedure kalde et Java-program?

  3. SQL Server-gendannelsesfejl - Adgang nægtes

  4. JPA SQL Server Ingen dialekttilknytning for JDBC-type:-9