Oracle
 sql >> Baza danych >  >> RDS >> Oracle

Jak wyodrębnić dane z trzech tabel za pomocą instrukcji relacyjnej obiektu SQL w Oracle 11g?

Brakuje kolumny custID w tabeli konta. Dodałem jeszcze kilka wierszy danych, aby utworzyć przypadek testowy dla Twoich wymagań.

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;

Wyjście:

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;

Wyjście:

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;

Wyjście:

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

Wyjście:

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



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Metadane funkcji wbudowanych Oracle

  2. pobierz wynik z zapytania

  3. Jaki jest skuteczny sposób śledzenia, identyfikowania i raportowania każdego „komunikatu o błędzie” zgłaszanego przez Twoją aplikację?

  4. Nazwa ORA-00955 jest już używana przez istniejący obiekt

  5. Zapytanie Oracle SQL - pobierz dane z ostatniego tygodnia (poniedziałek-sobota)