1. 程式人生 > >程式設計題 引港三張表的

程式設計題 引港三張表的

select * from o_trace_log;–業務流水 select * from o_mdm_ac_rel;–賬號與客戶介質表 卡和賬戶的 select * from o_dd_mst;–活期存款主檔案 一個賬戶存在多個賬號,一個客戶存在一個賬戶資訊 ac_type 客戶型別 1-4為對公客戶,5-6為個人賬戶; 流水錶中存放的資料資訊為當日的交易流水;

/*1、交易流水 中 找出賬戶為101214673(ac_id)的 卡號 交易 用子查詢的方式*/ select * from o_trace_log where ac_no in (select ac_no from o_mdm_ac_rel where ac_id=’101214673’); select * from o_trace_log where ac_id=’101214673’; ——————刪除重複資料: delete from o_trace_log t2 where t2.rowid< (select max(rowid) from o_trace_log t1 where t1.trace_no=t2.trace_no and t1.trace_cnt=t2.trace_cnt and t1.tx_date=t2.tx_date group by t1.trace_no,t1.trace_cnt,t1.tx_date)

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

–in select * from o_dd_mst d where d.ac_id in ( select ac_id from o_mdm_ac_rel m where name=’徐耀華’ );

/*3、當天沒有進行交易,且賬戶餘額超過1000元的賬戶 有多少人?用not exists實現 表連線(左外連線)實現*/ select count(*) from o_dd_mst A where not exists( —-賬戶不在交易流水錶的記錄內容 select ac_id from o_trace_log B where A.ac_id=B.ac_id ) and bal>1000;

–表連線(左外連線) select count(A.ac_id||A.ac_Seqn) from o_dd_mst A —count(*)也可以 left join o_trace_log B on A.ac_id=B.ac_id where A.bal>1000 and B.ac_id is null —B表的任意一個為空

/* select count(*) from o_dd_mst A where A.ac_id not in(select ac_id from o_trace_log B ) and bal>1000; */

/*4、開戶日期 最早的 第5人到第10人的資料, 去掉開戶日期為0的垃圾資料*/ 第二表 d.opn_date <>0 select b.* from ( select d.* ,rownum rw from (select * from o_dd_mst d where length(opn_date)=8 order by d.opn_date) d)b where b.rw between 5 and 10;

select * from (select round(rownum/2) rn,s.* from (select * from o_dd_mst d where length(opn_date)=8 order by d.opn_date) s) where rn between 3 and 5; ————分頁 select * from ( select ceil(rownum/15) page,t2.* from ( select * from o_dd_mst t1 where t1.opn_date<>0 order by t1.opn_date )t2 ) where page=2 /5、找出交易流水中名字長度大於4位的資料/ select * from o_trace_log where length(name)>4; lengthb(name) —表示儲存的位元組數 12個位元組 length(name)—–6個漢字 /*6、查詢過程中,把前三位為承德市的資料替換為 河北省承德市,只能用函式實現。*/ 第一張表也可以用 字串函式 replace(name,’承德市’,’河北省承德市’),–替換(x,old,new) concat(‘河北省’,name)–連線(x,y) from o_mdm_ac_rel where name like ‘承德市%’;

update o_mdm_ac_rel set name = ‘河北省’||name where name like ‘承德市%’;

————建立函式 create or replace function getnewname(f_name varchar) return varchar2 is v_name varchar2(60); begin ====================/注意偽表的使用,返回一列值 /。 select concat(‘河北省’,name) into v_name from dual where name like ‘承德市%’;

/*select case when substr(name,1,3) = ‘承德市’ then ‘河北省’ || name else name end into v_name from dual;*/ return v_name; end; 呼叫: o_trace_log 也可以 select getname(name),name from o_mdm_ac_rel where name like ‘承德市%’;

–根據部門編號,修改員工表的部門名稱 –(前提:員工表已新增部門名稱欄位dname) update emp set dname =(select dname from dept where emp.deptno=dept.deptno); alter table emp add(dname varchar2(20)); select * from emp; /*7、活期賬戶主檔案o_dd_mst 中,每一個機構下的當日平均餘額 ,要求展示開戶機構編碼,平均餘額1(小數位2位 不進位),平均餘額2(只展示整數,百位以上位 有資料) */ select to_char(trunc(avg(bal),2),99999.99) avg1, round(avg(bal),-2)avg2, avg(bal),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-7 對私 declare a varchar2(100):=100011359; b varchar2(100):=100150515; c varchar2(100):=’100150515,100011359’; cursor cc is select s.ac_type , m.name mname ,s.ac_id from o_dd_mst s ,o_mdm_ac_rel m where s.ac_id=m.ac_id and s.ac_id in (a,b); begin for cd in cc loop if cd.ac_type in (1,2,3,4) then –第三張表 dbms_output.put_line(‘客戶名稱’||cd.mname||’的帳戶’||cd.ac_id||’為對公帳戶’); else dbms_output.put_line(‘客戶名稱’||cd.mname||’的帳戶’||cd.ac_id||’為對私帳戶’); end if ; end loop; end; /*9、對於以下三個賬戶設定變數100930256、 100784563、101464960,查詢三個賬戶在 交易流水錶中的金額最大的一筆交易進行判別, 通過case-when的方法,如果金額小於4.5萬則 顯示無須授權操作,如果金額小於5萬且金額大於5萬 的90%,顯示接近授權操作,如果金額大於等於 5萬顯示該筆需要授權操作。 */ declare v_acid NUMBER(16,2):=(&ac_id); maxamt o_trace_log.amt%type; begin select max(amt) into maxamt from o_trace_log where ac_id=v_acid; case when maxamt <45000 then dbms_output.put_line(v_acid||’無須授權操作’); when maxamt >=45000 and maxamt<50000 then dbms_output.put_line(v_acid||’接近授權操作’); when maxamt >=50000 then dbms_output.put_line(v_acid||’該筆需要授權操作’); end case;

end;

declare v_acid1 varchar2(20):=’100930256’; v_acid2 varchar2(20):=’100784563’; v_acid3 varchar2(20):=’101464960’; cursor samt is select max(amt) maxamt ,ac_id from o_trace_log where ac_id in(v_acid1,v_acid2,v_acid3) group by ac_id; begin for ss in samt loop case when ss.maxamt <45000 then dbms_output.put_line(ss.ac_id||’無須授權操作’); when ss.maxamt >=45000 and ss.maxamt<50000 then dbms_output.put_line(ss.ac_id||’接近授權操作’); when ss.maxamt >=50000 then dbms_output.put_line(ss.ac_id||’該筆需要授權操作’); end case; end loop; end; —-注意以下的case-when結構,編寫case-when結構時,其中一定要有成立的條件,或者直接在case-when結構中加相應的else— declare v_acid1 varchar2(20):=’100930256’; v_acid2 varchar2(20):=’100784563’; v_acid3 varchar2(20):=’101464960’; maxamt number(9); acid number(9); begin select max(amt) into maxamt from o_trace_log where ac_id in(v_acid1,v_acid2,v_acid3);

select ac_id into acid from o_trace_log where ac_id in(v_acid1,v_acid2,v_acid3) and amt=maxamt; case when maxamt >50000 then dbms_output.put_line(maxamt||’=====’||acid||’該筆需要授權操作’); /* else dbms_output.put_line(maxamt||’=====’||acid||’===該筆需要授權操作’);*/ end case; end; /* 10、使用loop-end loop 查詢13082001機構下於 2015年建立的每個賬戶計算當天需要支付的利息 (可以剔除餘額為0), 進行列印,並列印本次支付利息的總金額。 (配合num進行迴圈) 第三張表 */ declare yue o_dd_mst.BAL%TYPE;–餘額 lilv o_dd_mst.RATE%TYPE;–利率 lixi number;–利息 acco o_dd_mst.ac_id%TYPE;–賬戶id summ number :=0; cursor cur is select ac_id,bal,rate,round(bal*(rate/365),3) from o_dd_mst where OPN_BR_NO=13082001 and substr(opn_date,0,4)=2015 and bal>0; begin –設定輸出視窗不存放快取資訊 dbms_output.enable(buffer_size=>null); open cur; LOOP fetch cur into acco,yue,lilv,lixi; exit when cur%notfound; summ:=summ+lixi; dbms_output.put_line(acco||’的利息為’||lixi); end loop; dbms_output.put_line(‘本次支付利息的總金額’||summ); close cur; end;

/* 11、使用while 迴圈,列印13020201下2015年1月活期賬戶的 客戶姓名,客戶編號、客戶的生日。 */ /*id_type 證件型別 1.身份證 2.戶口簿 3.護照 6.士兵證 7.港澳居民往來內地通行證 8:企業程式碼證;9:組織機構號;A營業執照;B:經營許可證;C: 事業法人證書 F.外國人居留證 G.警官證 H.其他證件 I.臺灣同胞來往內地通行證 J.軍官證 */ declare name1 varchar(100); acid number; birth varchar(6); cursor a is select name,cif_no, substr(id_no,11,4) from o_dd_mst mst,O_mdm_ac_rel rel where mst.ac_id=rel.ac_id and mst.opn_br_no=13020201 and substr(opn_date,1,6)=201501 and id_type=’1’ and length(id_no)=18; cursor b is select name,cif_no, substr(id_no,9,4) from o_dd_mst mst,O_mdm_ac_rel rel where mst.ac_id=rel.ac_id and mst.opn_br_no=13020201 and substr(opn_date,1,6)=201501 and id_type=’1’ and length(id_no)=15; begin dbms_output.enable(buffer_size=>null); open a; fetch a into name1,acid,birth; while a%found loop dbms_output.put_line(name1||’ ‘||acid||’ 生日 ‘||birth); fetch a into name1,acid,birth; end loop; close a; open b; fetch b into name1,acid,birth; while b%found loop dbms_output.put_line(name1||’ ‘||acid||’ 生日 ‘||birth); fetch b into name1,acid,birth; end loop; close b; end; /* 12、使用for迴圈,顯示交易流水錶中,當天累計交易金額 前50名的帳戶及其交易總金額, (增減標誌進行判別0是出帳1是入賬,區分對待);*/–12 declare cursor a is select ac_id,smt from (select rownum a,x.ac_id,x.smt from (select ac_id,sum(ABS(amt)) smt from o_trace_log where ac_id <>0 group by ac_id order by smt desc) x) where a<=50;

cursor aa is select add_ind,sum(ABS(amt)) x from o_trace_log where ac_id in(select ac_id from (select rownum a,x.ac_id from (select ac_id from o_trace_log where ac_id <>0 group by ac_id order by sum(ABS(amt)) desc) x) where a<=50) group by add_ind;

m number; n number; begin for b in a loop dbms_output.put_line(‘賬戶’||b.ac_id||’交易總金額’||b.smt); end loop; for bb in aa loop if bb.add_ind=1 then dbms_output.put_line(‘入賬總金額’||bb.x); m:=bb.x; else dbms_output.put_line(‘出賬總金額’||bb.x); n:=bb.x; end if; end loop; dbms_output.put_line(‘交易總金額’||(m+n)); end; /* 13、1995年之前開戶的客戶編碼,客戶名稱及 該客戶的賬戶資訊,卡號資訊必須保證遊標使用兩種。*/ declare name1 Varchar2(60); acid number(9); cifno varchar(20); acno varchar(20); –查詢95年之前的開戶的賬戶資訊 cursor b is select m.name,m.ac_id,d.cif_no,m.ac_no –姓名,賬戶ID,客戶號,賬卡號 from o_mdm_ac_rel m, o_dd_mst d where m.ac_id=d.ac_id and substr(OPN_DATE,1,4)<1995; begin OPEN b; LOOP FETCH b into name1,acid,cifno,acno; EXIT WHEN b%NOTFOUND; dbms_output.put_line(name1||acid||’==’||cifno||’==’||acno); END LOOP; CLOSE b; end;

–查詢95年之前的開戶的客戶資訊 declare cursor cur is select m.* from (select d1.*, d2.cif_no from o_dd_mst d1, (select distinct cif_no from o_dd_mst where substr(OPN_DATE, 1, 4) < 1995) d2 where d1.cif_no = d2.cif_no) d3, o_mdm_ac_rel m where d3.ac_id = m.ac_id ; begin –此處為迴圈 end; /*14、建立一個函式,通過傳入卡號,獲取客戶號 應用在交易流水中,查詢資料時,可以使用該函式 獲取客戶編碼。*/ create or replace function getcustno(a number) return number is b number(20); begin select m.cif_no into b from o_dd_mst m, –客戶號 o_mdm_ac_rel n where m.ac_id = n.ac_id and n.ac_no=a;–卡號 return b; end;

select getcustno(ac_no) from o_trace_log; –呼叫不出來,無效數字

select t1.cif_no 客戶號,t2.name 客戶名稱, wm_concat(t1.ac_id) 賬戶號,wm_concat(t1.bal) 賬戶餘額 from o_dd_mst t1 left join o_mdm_ac_rel t2 on t1.ac_id=t2.ac_id and t1.ac_seqn=t2.ac_seqn where cif_no = 10066744 group by t1.cif_no,t2.name

select max(t1.cif_no) 客戶號,max(t2.name) 客戶名稱, sum(decode(rownum,1,t1.ac_id,0)) 賬戶號1, sum(decode(rownum,1,t1.bal,0)) 賬戶餘額, sum(decode(rownum,2,t1.ac_id,0)) 賬戶號2, sum(decode(rownum,2,t1.bal,0)) 賬戶餘額, sum(decode(rownum,3,t1.ac_id,0)) 賬戶號3, sum(decode(rownum,3,t1.bal,0)) 賬戶餘額, sum(decode(rownum,4,t1.ac_id,0)) 賬戶號4, sum(decode(rownum,4,t1.bal,0)) 賬戶餘額 from o_dd_mst t1 left join o_mdm_ac_rel t2 on t1.ac_id=t2.ac_id and t1.ac_seqn=t2.ac_seqn where cif_no = 10066744 group by t1.cif_no