1109课后作业

一、customer-account-trade

客户表customer

字段数据类型描述
cust_idvarchar2客户号
cust_nmvarchar2客户名称
sexvarchar2性别
companyvarchar2工作单位
cust_idcust_nmsexcompany
1000100001任正非华为
1000100002马云阿里
1000100003董明珠格力
1000100004马化腾腾讯
1000100005李彦宏百度

客户账户表account
一个客户可存在多个账号

字段数据类型描述
cust_idvarchar2客户号
acct_idvarchar2账号
cur_balnumber余额
cust_idacct_idcur_bal
100010000170127630500,000
1000100001621420221500,000
1000100002603510563000,000
1000100003701286005000,000
1000100003701312081000,000

账户交易表trade

字段数据类型描述
acct_idvarchar2账号
txn_dtdate交易日期
txn_amtnumber交易金额
acct_idtxn_dttxn_amt
7012763020220101200,000
603510562022010250,000
701286002022010210,000
70131208202202043000
701276302022020560,000

1、请写出sql,查询当前余额大于100w的客户

select t1.cust_id
from account t1
group by t1.cust_id
having sum(t1.cur_bal) > 1000000;

2、请写出sql,查询2022年1月交易金额大于10w的客户

select t1.cust_id
from account t1
inner join trade t2
        on t1.acct_id = t2.acct_id
        and to_char(t2.txn_dt, 'yyyymm') = '202201'
group by t1.cust_id
having sum(t2.txn_amt) > 100000; 

3、请写出sql,查询每个客户交易金额最大的日期及金额

select t3.cust_id
      ,t3.txn_amt
      ,t3.txn_dt
from (
	select t1.cust_id
	      ,t2.txn_amt
	      ,t2.txn_dt
	      --,max(t2.txn_amt)over(partition by t1.cust_id) as max_txn_amt
	      ,rank()over(partition by t1.cust_id order by t2.txn_amt desc) as rn
	from account t1
	inner join trade t2
	        on t1.acct_id = t2.acct_id
     ) t3
--where t3.txn_amt = t3.max_txn_amt; 
where t3.rn = 1;

4、请写出sql,查询每个客户交易日期最近的日期及金额

select t3.cust_id
      ,t3.txn_amt
      ,t3.txn_dt
from (
	select t1.cust_id
	      ,t2.txn_amt
	      ,t2.txn_dt
	      --,max(t2.txn_dt)over(partition by t1.cust_id) as max_txn_dt
	      ,rank()over(partition by t1.cust_id order by t2.txn_dt desc) as rn
	from account t1
	inner join trade t2
	        on t1.acct_id = t2.acct_id
     ) t3
--where t3.txn_dt = t3.max_txn_dt; 
where t3.rn = 1;

二、申请表apply_list\放款表loan_list

字段数据类型描述
apply_datedate申请日期
loan_novarchar2合同编号
apply_prinnumber申请金额
resultvarchar2审批结果
apply_dateloan_noapply_prinresult
2022/2/5gm29011410000pass
2022/2/5gm29014010000pass
2022/2/5gm29014410000pass
2022/3/1gm29092310000reject
2022/3/1gm29093710000reject
2022/3/1gm29093810000pass
2022/4/17gm295718000pass

1、请写出sql,查询每天的审批通过率及审批通过的平均申请金额

select t2.apply_day
      ,round(t2.sum_apply_pass / t2.count_apply * 100, 2) || '%' as pass_rate
      ,t2.sum_apply_prin / t2.sum_apply_pass as avg_apply_pass_prin
from (
	select truncate(t1.apply_date,'dd') as apply_day
          ,sum(case when t1.result = 'pass' then 1 else 0 end) as sum_apply_pass
          ,count(1) as count_apply
          ,sum(case when t1.result = 'pass' then t1.apply_prin else 0 end) as sum_apply_prin
from apply_list t1
    group by truncate(t1.apply_date,'dd');
	 ) t2

放款表loan_list

字段数据类型描述
loan_datedate放款日期
loan_novarchar2合同编号
id_novarchar2身份证号
loan_prinnumber放款金额
paid_principalnumber已还本金
product_ratevarchar2消费等级
overdue_daysnumber逾期天数
loan_dateloan_noid_noloan_prinpaid_principalproduct_rateoverdue_days
2022/2/5gm2901441100001990100008000anull
2022/4/17gm296833550000199280001500d11
2022/5/11gm310938230000199160005500dnull
2022/5/18gm350939450000198910000b432
2022/4/18gm296834510000199260001500d31
2022/4/20gm29689451000019826000015000d40
2022/3/20gm29687451000019871300010000d60

2、请写出sql,查询2022年2-5月份,不同消费等级的放款笔数、放款金额、30天以上金额逾期率(剩余本金/放款金额)

select t1.product_rate
      ,count(1) as count_loan
      ,sum(t1.loan_prin) as sum_loan_prin
      --,sum(case when nvl(t1.overdue_days,0) > 30 then t1.loan_prin - t1.paid_principal else 0 end) as sum_overdue_prin
      --,sum(case when nvl(t1.overdue_days,0) > 30 then t1.loan_prin else 0 end ) as  sum_loan_prin_of_overdue
      --,round(sum(case when nvl(t1.overdue_days,0) > 30 then t1.loan_prin - t1.paid_principal else 0 end) / sum(case when nvl(t1.overdue_days,0) > 30 then t1.loan_prin end) * 100, 2) || '%' as overdue_rate
      ,nvl(round(sum(case when nvl(t1.overdue_days,0) > 30 then t1.loan_prin - t1.paid_principal else 0 end) / sum(case when nvl(t1.overdue_days,0) > 30 then t1.loan_prin end) * 100, 2),0) || '%' as overdue_rate
from loan_list t1
where to_char(t1.loan_date, 'yyyymm') in ('202202','202203','202203','202204','202205') 
group by t1.product_rate;
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只会HelloWorld的华娃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值