1. 程式人生 > >oracle基礎案例(1)

oracle基礎案例(1)

select * from o_mdm_ac_rel   --賬號與客戶介質表
select * from o_dd_mst       --活期存款主檔案
select * from o_trace_log    --業務流水
(一個賬戶存在多個賬號,一個客戶存在一個賬號資訊)
--1.交易流水中找出賬戶為101214673的卡號交易用子查詢的方式

select * from o_trace_log where ac_id=101214673;

select * from o_trace_log where ac_no in
 (select ac_no from o_mdm_ac_rel where ac_id=101214673)

--2.在活期賬戶主檔案中找出徐耀華的賬戶用exists實現
select d.* from o_dd_mst d where exists
   (select * from o_mdm_ac_rel m where m.name='徐耀華' and d.ac_id=m.ac_id)

--3.當天沒有進行交易,且賬戶餘額超過1000元的賬戶有多少人?用not exists實現
select  count(*) from o_dd_mst m where not exists
  (select  ac_id from o_trace_log t where t.ac_id=m.ac_id  )
and bal>1000
 
--4.開戶日期最早的第5人到第10人的資料
select s.* from 
 (select rownum rn,e.* from
 (select d.* from o_dd_mst d where length(opn_date)=8 order by d.opn_date) e)s
where rn between 5 and 10;

--5.找出交易流水中名字長度大於4位的資料
select * from o_trace_log where length(name)>4;


--6.查詢過程中,把前三位為承德市的資料替換為河北省承德市,只能用函式實現。

select replace(name,'承德市','河北省承德市'),concat('河北省',name)
from o_mdm_ac_rel  where name like '承德市%';


--7.活期賬戶主檔案中,每一個機構下的當日平均餘額,要求展示開戶機構編碼,

--平均餘額1(小數位2位不進位),平均餘額2(只展示整數,百位以上位有資料)

select trunc(avg(bal),2) 平均餘額1,trunc(avg(bal),-2) 平均餘額2,
opn_br_no from o_dd_mst group by opn_br_no;


--8.寫出以個plsql塊,定義變數分別為100011359,100150515,通過if-then的方法,
--判別賬戶是對公賬戶還是對私賬戶,並打印出結果(客戶名稱XXX的賬戶XXX為對公賬戶)。

ac_type 1-4 對公  5-6  對私

select * from o_mdm_ac_rel   --賬號與客戶介質表
select * from o_dd_mst       --活期存款主檔案
select * from o_trace_log    --業務流水
declare
  name1 o_mdm_ac_rel.name%type;
  ac_id1 o_mdm_ac_rel.ac_id%type;
  ac_type1 o_mdm_ac_rel.ac_type%type;
begin
  select name.as_id,ac_type into name1,ac_id1,ac_type1 from o_mdm_ac_rel m
  where ac_id='&id';
 if ac_type1=5 or as_type1=6 then
   dbms_output.put_line('客戶名稱' ||m.name||'的賬戶' ||m.ac_id1||'為對私賬戶');
 else
   dbms_output.put_line('客戶名稱' ||m.name||'的賬戶' ||m.ac_id1||'為對私賬戶');
 end if;
end;
 
--9.對於以下三個賬戶設定變數100930256、100784563、101464960,查詢三個賬戶
--在交易流水錶中的金額最大的一筆交易進行判別,通過case-when的方法,
--如果金額小於5萬則顯示無須授權操作,如果金額小於5萬且金額大於5萬的90%,

--顯示接近授權操作,如果金額大於等於5萬顯示該筆需要授權操作。  

declare 
 max_amt o_trace_log.amt%type;
 cursor a is select ac_id,max(amt) max_amt from o_trace_log
  where ac_id='100930256' or ac_id='100784563' or ac_id='101464960'
  group by ac_id;
begin
 for b in a loop
  case 
   when b.max_amt<5000 and b.max_amt>4500 then dbms_output.put_line('接近授權');
   when b.max_amt>=5000 then dbms_output.put_line('需要授權操作');
   when b.max_amt<5000 then dbms_output.put_line('無授權操作');
  end case;
 end loop;
end;    --編寫case -when 結構時,其中一定要有成立的條件,
        --或者直接在case—when結構中加相應的else

--10.使用loop-end loop 查詢13082001機構下於2015年建立的
--每個賬戶計算當天需要支付的利息(可以剔除餘額為0),

--進行列印,並列印本次支付利息的總金額。(配合num進行迴圈) 

declare
ac_id1 o_mdm_ac_rel.ac_id%type;
lixi1 number(20);
cursor a is select ac_id,sum(bal*0.0072/365) lixi from o_dd_mst
where ac_id in(select m.ac_id from o_mdm_ac_rel where 
    opn_br=13082001 and substr(beg_date,1,4)=2015) group by ac_id;
begin
open a;
loop
fetch a into ac_id1,lixi1;
dbms_output.put_line('賬戶為'||ac_id1||'的需要付利息'||lixi1);
exit when a%notfound;
end loop;
close a;
end;

--11、使用while 迴圈,列印13020201下2015年1月活期賬戶的

--客戶姓名,客戶編號、客戶的生日。

declare 
cursor bbb is select name,AC_ID,substr(id_on,7,8) from o_mdm_ac_rel where OPN_BR_NO=13020201
and substr(beg_date,1,6)=201501;
name1 o_mdm_ac_rel.name%type;
ac_id1 o_mdm_ac_rel.ac_id%type;
id_no1 o_mdm_ac_rel.id_no%type;
begin
oppn bbb;
fetch bbb into name1,ac_id1,id_no1;
while bbb%found loop
dbms_output.put_line('客戶姓名'||name1||',客戶編號'||ac_id1||',客戶生日'||id_no1);
fetch bbb into name1,ac_id1,id_no1;
end loop;
close bbb;
end;

--12、使用for迴圈,顯示交易流水錶中,當天累計交易金額前50名的帳戶

--及其交易總金額,(增減標誌進行判別0是出帳1是入賬,區分對待);

declare
 cursor ccc is select * from (select a.*,rownum r from
  (select ac_id,sum(amt) sum_amt from o_trace_log 
    group by ac_id order by sum_amt desc)a) where r<=50;
begin
 for ddd in ccc loop
   dbms_output.put_line('賬號'||ddd.ac_id||',交易總金額'||ddd.sum_amt);
 end loop;
end;

--13、1995年之前開戶的客戶編碼,客戶名稱及該客戶的賬戶資訊,卡號資訊

--必須保證遊標使用兩種。

select * from o_dd_mst       --日期 OPN_DATE--活期存款主檔案  

declare
cursor c_yb is  select * from 
   (select d.cif_no ,m.name ,m.ac_id ,m.ac_no 
   from o_dd_mst d left join o_mdm_ac_rel m on d.ac_id=m.ac_id
    where opn_date<1995);
begin
  for a in c_yb loop
  dbms_output.put_line(a.cif_no ||':客戶編號  '||a.name||
             ':客戶姓名  '||a.ac_id ||':賬號id  '||a.ac_no ||':卡號資訊');
  end loop;
end;
         
--14、建立一個函式,通過傳入卡號,獲取客戶號

--應用在交易流水中,查詢資料時,可以使用該函式獲取客戶編碼。

create or replace function fc_yh(f_ac_no o_mdm_ac_rel.ac_no%type)
return number       
is 
  k_cif_no o_dd_mst.cif_no%type;
begin
  select d.cif_no into k_cif_no from o_mdm_ac_rel m,o_dd_mst d 
  where m.ac_id=d.ac_id and ac_no=f_ac_no; 
return k_cif_no;  
end; 
   select fc_yh(ac_no) from o_mdm_ac_rel