每天一道大厂SQL题【Day15】微众银行真题实战(五)_sql题目五 单户5000万以上贷款大户(如客户为集团客户则需汇总为1个客户进行判定)

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

values (‘20201101’, ‘s000’, ‘u000’, 1, 1, 1, 700, ‘2020-11-01 08:12:12’),
(‘20201102’, ‘s088’, ‘u088’, 1, 1, 1, 888, ‘2020-11-02 08:12:12’),
(‘20201230’, ‘s091’, ‘u091’, 1, 1, 1, 789, ‘2020-12-30 08:12:12’),
(‘20201230’, ‘s092’, ‘u092’, 1, 0, 0, 0, ‘2020-12-30 08:12:12’),
(‘20201230’, ‘s093’, ‘u093’, 1, 1, 1, 700, ‘2020-12-30 08:12:12’),
(‘20201231’, ‘s094’, ‘u094’, 1, 1, 1, 789, ‘2020-12-31 08:12:12’),
(‘20201231’, ‘s095’, ‘u095’, 1, 1, 1, 600, ‘2020-12-31 08:12:12’),
(‘20201231’, ‘s096’, ‘u096’, 1, 1, 0, 0, ‘2020-12-31 08:12:12’)
;
–创建核额流水表
drop table if exists check_t;
create table check_t (
sno string comment ‘流水号’,
uid string,
is_risk_apply bigint,
is_pass_rule bigint,
is_obtain_qutoa bigint,
quota decimal(30,6), update_time string
) partitioned by (ds string comment ‘日期分区’);
–动态分区需要设置
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table check_t partition (ds) select sno,
uid, is_risk_apply, is_pass_rule, is_obtain_qutoa, quota,
update_time,
ds
from check_view;


![img](https://img-blog.csdnimg.cn/img_convert/aee0381a010bb999b2d180e113f3828c.png)



– 创 建 借 据 表
create table debt(
duebill_id string comment ‘借据号’,
uid string, prod_type string,
putout_date string,
putout_amt decimal(30, 6),
balance decimal(30, 6),
is_buliang int,
overduedays int
)partitioned by (ds string comment ‘日期分区’);
–资料提供了一个34899条借据数据的文件
–下面补充如何将文件的数据导入到分区表中。需要一个中间普通表过度。
drop table if exists webank_db.debt_temp;
create table webank_db.debt_temp(
duebill_id string comment ‘借据号’, uid string,
prod_type string,
putout_date string, putout_amt decimal(30, 6),
balance decimal(30,6),
is_buliang int, overduedays int,
ds string comment ‘日期分区’
) row format delimited fields terminated by ‘t’;

load data local inpath ‘/root/debt.txt’ overwrite into table webank_db.debt_temp;

–动态分区需要设置
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table webank_db.debt partition (ds)
select * from webank_db.debt_temp;

–技巧:如果查询debt表,由于分区数太多,导致查询很慢。
– 开发阶段,我们可以事先将表缓存起来,并且降低分区数比如为6,那么查缓存表大大提升了开发效率。
– 上线阶段,再用实际表替换缓存表。
–首次缓存会耗时慢
cache table cache_debt as select /+ coalesce(6) / from
debt;
–第二次使用缓存会很快
select count(*) from cache_debt;
select ds,count(1) from cache_debt group by ds;


### 思路分析


![image-20230306194326674](https://maynor-demo.oss-cn-shenzhen.aliyuncs.com/img/image-20230306194326674.png)
![image-20230306194357290](https://maynor-demo.oss-cn-shenzhen.aliyuncs.com/img/image-20230306194357290.png)
随机观察2个借据的情况



*select* * *from* cache_debt *where* duebill_id=‘u001-1’ *order by* ds;

*select* * *from* cache_debt *where* duebill_id=‘u005-2’ *order by* ds;


![image-20230306194851164](https://img-blog.csdnimg.cn/img_convert/15c5b9dc6e6f9fbeb5d04a51d99968a7.png)


![image-20230306194900444](https://img-blog.csdnimg.cn/img_convert/b631f0c3f44cbb0dcaccdab26b234402.png)


分3个大步骤:


步骤1:形成临时表1\_每月的总发放金额




| 发放月份 | 发放金额 |
| --- | --- |
| 2019-10 | aa |
| 2019-11 | bb |
| 2019-12 | cc |
| 2020-01 | dd |
| 2020-02 | ee |
| 2020-03 | ff |
| 2020-04 | gg |
| 2020-05 | hh |


步骤2:形成临时表2\_发放后第几个月末时的不良余额




|  | 发放后第几个月末时的不良余额(元) |  |  |  |  |  |  |  |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 发放月份 | 1月后 | 2月后 | 3月后 | 4月后 | 5月后 | 6月后 | 7月后 | 8月后 |
| 2019-10 | a1 | a2 | a3 | a4 | a5 | a6 | a7 | a8 |
| 2019-11 | b1 | b2 | b3 | b4 | b5 | b6 | b7 | b8 |
| 2019-12 | c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8 |
| 2020-01 | d1 | d2 | d3 | d4 | d5 | d6 | d7 | d8 |
| 2020-02 | e1 | e2 | e3 | e4 | e5 | e6 | e7 | e8 |
| 2020-03 | f1 | f2 | f3 | f4 | f5 | f6 | f7 | f8 |
| 2020-04 | g1 | g2 | g3 | g4 | g5 | g6 | g7 | g8 |
| 2020-05 | h1 | h2 | h3 | h4 | h5 | h6 | h7 | h8 |


步骤3:用上面的临时表1关联临时表2,用临时表2的每个值除以临时表的总金额。




|  |  | 发放后第几个月末时的不良余额占发放金额的比例 |  |  |  |  |  |  |  |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 发放月份 | 发放金额 | 1月后 | 2月后 | 3月后 | 4月后 | 5月后 | 6月后 | 7月后 | 8月后 |
| 2019-10 | aa | a1/aa | a2/aa | a3/aa | a4/aa | a5/aa | a6/aa | a7/aa | a8/aa |
| 2019-11 | bb | b1/bb | b2/bb | b3/bb | b4/bb | b5/bb | b6/bb | b7/bb | b8/bb |
| 2019-12 | cc | c1/cc | c2/cc | c3/cc | c4/cc | c5/cc | c6/cc | c7/cc | c8/cc |
| 2020-01 | dd | d1/dd | d2/dd | d3/dd | d4/dd | d5/dd | d6/dd | d7/dd | d8/dd |
| 2020-02 | ee | e1/ee | e2/ee | e3/ee | e4/ee | e5/ee | e6/ee | e7/ee | e8/ee |
| 2020-03 | ff | f1/ff | f2/ff | f3/ff | f4/ff | f5/ff | f6/ff | f7/ff | f8/ff |
| 2020-04 | gg | g1/gg | g2/gg | g3/gg | g4/gg | g5/gg | g6/gg | g7/gg | g8/gg |
| 2020-05 | hh | h1/hh | h2/hh | h3/hh | h4/hh | h5/hh | h6/hh | h7/hh | h8/hh |


### 答案获取


建议你先动脑思考,动手写一写再对照看下答案,如果实在不懂可以点击下方卡片,回复:`大厂sql` 即可。  
 参考答案适用HQL,SparkSQL,FlinkSQL,即大数据组件,其他SQL需自行修改。


### 加技术群讨论


点击下方卡片关注 联系我进群


或者直接`私信我进群`


#### 微众银行源数据表附录:


1. 核额流水表




| 字段名 | 字段意义 | 字段类型 |
| --- | --- | --- |
| ds | 日期分区,样例格式为20200101,每个分区有全量流水 | string |
| sno | 每个ds内主键,流水号 | string |
| uid | 户id | string |
| is\_risk\_apply | 是否核额申请(核额漏斗第一步)取值0和1 | bigint |
| is\_pass\_rule | 是否通过规则(核额漏斗第二步)取值0和1 | bigint |
| is\_obtain\_qutoa | 是否授信成功(核额漏斗第三步)取值0和1 | bigint |
| quota | 授信金额 | decimal(30,6) |
| update\_time | 更新时间样例格式为2020-11-14 08:12:12 | string |


2. 借据表





![img](https://img-blog.csdnimg.cn/img_convert/51411b681f6902ffe13a49f8c3a28fc3.png)
![img](https://img-blog.csdnimg.cn/img_convert/336de8cc4ba8f232e69f20a7601ce58a.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 11
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值