sql >> Database >  >> RDS >> Oracle

Hoe gegevens uit drie tabellen te extraheren met behulp van de relationele instructie SQL-objecten in oracle 11g

U mist de kolom custID in de rekeningtabel. Ik heb nog een paar rijen met gegevens toegevoegd om de testcase voor uw vereiste te maken.

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;

Uitgang:

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;

Uitgang:

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;

Uitgang:

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';

Uitgang:

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



  1. Hoe grafieken en diagrammen te genereren uit de mysql-database in php

  2. PostgreSQL:is het mogelijk om een ​​aangepaste naam op te geven voor PRIMARY KEY of UNIQUE?

  3. Sneller om veel resultaten willekeurig te verdelen op MySQL Server of in PHP

  4. Hoe CREATE INDEX correct te doen met SQLite