====================================
1.开户日期排序,去掉开户日期为0的垃圾数据,设置 sql中展示条数,和页数,展示要求的数据信息
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
第二种方式:
Select rownum as num ,t1.* from
(select * from o_dd_mst t where t.opn_date<>0 order by t.opn_date )t1 )t2 where t2.num >15*(6-1)and t2.num <=15*6
2.行转列
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
方式二:子查询 多表关联 应该是第一种考虑方式
Select distinct t1.cif_no ,t2.name ,t3.ac_id,t3.bal
from o_dd_mst t1,o_mdm_ac_rel t2
(select ac_id,bal,cif_no from o_dd_mst t1 where cif_no = 10066744) t3,
(select ac_id,bal,cif_no from o_dd_mst t1 where cif_no = 10066744) t4,
【银行三张表 作业()】where t1.ac_id=t2.ac_id
and cif_no = 10066744
and t3.cifno=t1.cifno
And t3.cifno<>t4.cifno
group by t1.cif_no
=====================================
1.当天在交易流水中发生的35岁到60岁之间的女性客户,累计出账交易金额超10万的交易信息统计.展示交易客户的客户编号,客户名称和交易总金额
正确思路:
select t3.cif_no,t2.name,sum(t1.amt) from
o_trace_log t1 以交易表为主表
o_mdm_ac_rel t2 取身份证号
o_dd_mst t3 取客户名称
都以ac_id关联条件 t1=t2 t2=t3
判断年龄和女性客户
and length(t2.id_no)=18
and to_number(substr(t2.id_no,7,4)) < 2018 - 35
and to_number(substr(t2.id_no,7,4)) > 2018 - 60
and mod(to_number(substr(id_no,-2,1)),2) = 0
and t1.add_ind= ‘0’
group by t3.cif_no,t2.name
having sum(t2.amt)>100000;
select t3.cif_no 客户号,
max(t1.name) 客户名称,
sum(t2.amt) 交易总金额
from o_mdm_ac_rel t1
left join o_trace_log t2 on t1.ac_id=t2.ac_id
left join o_dd_mst t3 on t1.ac_id=t3.ac_id
where t1.id_type = ‘1’ —身份证
and length(t1.id_no)=18 —-18位的
and mod(substr(id_no,-2,1),2)=0 —-女性客户
and (extract(year from sysdate) - to_number(substr(id_no,7,4)))
between 35 and 60 —-年龄
and t2.add_ind=0 –出账
group by t3.cif_no
having sum(t2.amt)>=100000;
2.统计各行年龄(65岁以上)客户账户余额占比在2%以上的机构编号,占比数量
正确思路:
select opn_br_no,t3.num/t4.num from
(select t1.opn_br_no ,sum(t1.bal) from
o_dd_mst t1
o_mdm_ac_rel t2,
t1,t2 ac_id相等
and length(t2.id_no)=18
and to_number(substr(t1.id_no,7,4))< 2018 - 65
group by t1.opn_br_no ) t3, —-先找老龄客户的占比
(select t1.opn_br_no ,sum(t1.bal) from
o_dd_mst t1
o_mdm_ac_rel t2,
t1,t2 ac_id相等
group by t1.opn_br_no ) t4 —-全行的余额统计
where t3,t4 opn_br_no机构一致
select m.opn_br_no 开户机构编码,
m.a/n.b 占比
from (
select t1.opn_br_no,
sum(t2.bal) a
from o_mdm_ac_rel t1,
o_dd_mst t2
where t1.ac_id=t2.ac_id
and t1.id_type=’1’
and length(t1.id_no)=18
and extract(year from sysdate)-substr(t1.id_no,7,4)>65
group by t1.opn_br_no ) m,
(select sum(bal) b, —总行的余额
opn_br_no
from o_dd_mst
group by opn_br_no ) n
where m.opn_br_no=n.opn_br_no
and m.a/n.b > 0.02;
============面试智力题======================
select * from ( —–查看具体的一毛,五角,一元的个数
select x,y,z,x+y+z from
(select * from
(select rownum as x from o_dd_mst
where rownum <=20)t1, —-一毛钱硬币的个数
(select rownum as y from o_dd_mst
where rownum <=20)t2,—-五角硬币的个数
(select rownum as z from o_dd_mst
where rownum <=20)t3—-一元硬币的个数
where 1*x+5*y+10*z =50 —公式 金额总值5元
and t1.x<=18 —一毛钱限制的个数
and t2.y<=2 —五角限制的个数
order by x+y+z desc ) )
where rownum=1
==========================================
给定一个字符串,写算法 获取字符串的最大连续重复字符数。
abcdaaaabcdcc
两层循环嵌套
第一位在字符串里是否包含,如果找到就是重复的,正确的
接着再看
第一和二位 在后面是否存在,
第一二三位是否存在,
第一二三四位是否存在,不存在就跳出循环
declare
h_string varchar(20):=’aaaaaaaabcdcc’;
c_string varchar(20):=”;
–查找的
c_num number :=0;
t_num number :=0;
–最长的长度
begin
for x in 1..length(h_string) –从第一到结束位取所有的位数 外层表示循环次数
loop
for y in 1..length(h_string) - x —x..length(h_string)也可以
loop —–里层循环查看是否有重复的
c_string:=substr(h_string,x,y);
c_num:= instr(h_string,c_string,x+1);
—找后面的位置是否重复
if c_num !=0 and length(c_string) > t_num then
t_num:=length(c_string);
end if;
end loop;
end loop;
dbms_output.put_line(‘重复字符串的最大长度’||t_num);
end;
坚强:
–扩展题:给定一个字符串,写算法获取字符串的最大连续重复字符数。
–如:字符串一:abcdbaaaabcdcc,他们最大连续重复字符为abcd,最大连续重复字符数为4个
create or replace procedure pro_get_char(input_char varchar2)
is
output_char varchar2(200);
x number;
len number:=0;
begin
for i in 1..length(input_char) loop
for j in 1..((length(input_char)-i)/2) loop
select instr(input_char,substr(input_char,i,j),1,2) into x from dual;
if x!=0 and length(substr(input_char,i,j))>len then
output_char:=substr(input_char,i,j);
len:=length(substr(input_char,i,j));
end if;
end loop;
end loop;
dbms_output.put_line(output_char||’–’||len);
end;
call pro_get_char(‘abcdeghrfbcdeght’)
=========触发器=============================
1.如果要更新dept表中的deptno主键内容,
判断员工表中是否有该部门员工?
如果存在则不允许修改;
2.如果要删除dept表的信息,
判断员工是否有该部门,
有则先删除该部门下的员工。
没有什么也不做
CREATE OR REPLACE TRIGGER AAA
BEFORE DELETE OR UPDATE OF DEPTNO
ON DEPT
FOR EACH ROW
declare
num number(30);
BEGIN
select count(*) into num from
emp
where deptno = :old.deptno;
case when UPDATING(‘deptno’)
then
if num > 0 then
RAISE_APPLICATION_ERROR(-20002,’不允许删除’);
end if;
when DELETING THEN
DELETE FROM EMP WHERE DEPTNO = :old.deptno;
END CASE ;
END ;
要求:写一个存储过程 usp_InsertATMUsersData,
对 userInfo 表模拟插入用户记录,
每种用户类型(ATM 取款机,营业员)各 5 条(要求使用循环)
/创建表:/
create table userinfo –操作员,atm机器信息表
(userid number not null, –操作员编号,unique identifier,主键
usertype varchar2(20) not null, –操作员类型,必填,atm取款机,营业员
code varchar2(50) –代号信息 操作员或营业员工号 );
/营业员编号的序列生成:1开头的四位数字/
create sequence yyy_seq
start with 1000
increment by 1
maxvalue 1999
minvalue 1000
nocycle
cache 5
/ATM取款机编号的序列的生成:2开头的四位数字/
create sequence qkj_seq
start with 2000
increment by 1
maxvalue 2999
minvalue 2000
nocycle
cache 5
/创建存储过程:/
create or replace procedure usp_InsertATMUsersData
as
i number default 1;
v_yyy userinfo.userid%type;
v_qkj userinfo.userid%type;
yyy_code userinfo.code%type;
qkj_code userinfo.code%type;
begin
while i<=5 loop
i:=i+1;
select yyy_seq.nextval into v_yyy from dual;
yyy_code :=’营业员’||v_yyy;
insert into userinfo(userid,usertype,code)
values (v_yyy,’营业员’,yyy_code);
select qkj_seq.nextval into v_qkj from dual;
qkj_code :=’ATM取款机’||v_qkj;
insert into userinfo(userid,usertype,code)
values (v_qkj,’ATM取款机’,qkj_code);
end loop;
end;
存储过程已创建,调用:
call usp_InsertATMUsersData();
select * from userinfo;