每天一道大厂SQL题【Day14】微众银行真题实战(四)_基于附录 3《模型输出表》统计下述指标,请提供统计 sql(备注 value 值为 1 时即命(2)

img
img
img

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

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

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

大家好,我是Maynor。相信大家和我一样,都有一个大厂梦,作为一名资深大数据选手,深知SQL重要性,接下来我准备用100天时间,基于大数据岗面试中的经典SQL题,以每日1题的形式,带你过一遍热门SQL题并给出恰如其分的解答。

一路走来,随着问题加深,发现不会的也愈来愈多。但底气着实足了不少,相信不少朋友和我一样,日积月累才是最有效的学习方式!

每日语录

生活中很多人喜欢小题大作,其实真的没有必要,要想想大题怎么办。

C:\Users\ADMINI~1\AppData\Local\TemputoolsDoutuPlugin/tempImage1677723931267.gif

第14题:

需求列表

基于附录3《模型输出表》统计下述指标,请提供统计SQL(备注: value值为1时即命中)

统计日期统计指标命中户数命中率
20201010/累计V01
V02
V03
V04
V05
V06
数据准备

链接:https://pan.baidu.com/s/1Wiv-LVYziVxm8f0Lbt38Gw?pwd=s4qc
提取码:s4qc

debt.txt文件

set spark.sql.shuffle.partitions=4;
create database webank_db;
use webank_db;
create or replace temporary view check_view (ds comment '日期分区',
sno comment '流水号', uid comment '用户id',
is_risk_apply comment '是否核额申请',
is_pass_rule comment '是否通过规则',
is_obtain_qutoa comment '是否授信成功', quota comment '授信金额',
update_time comment '更新时间')
as
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

-- 创 建 借 据 表
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;

思路分析

--创建模型输出表数据集 
create or replace temporary view output_model(ds,sno,create_time,uid,content,update_time) as values
('20200101','s001','20210101','u001','{"V01":0,"V02":1,"V03":1,"V04":0,"V05":1,"V06":0}','20210101'),
('20200101','s002','20210101','u002','{"V01":1,"V02":1,"V03":0,"V04":1,"V05":0,"V06":0}','20210101'),
('20200101','s003','20210101','u003','{"V01":0,"V02":1,"V03":0,"V04":1,"V05":0,"V06":1}','20210101'),
('20200101','s004','20210101','u004','{"V01":0,"V02":1,"V03":1,"V04":1,"V05":1,"V06":1}','20210101'),
('20200101','s005','20210101','u005','{"V01":0,"V02":0,"V03":0,"V04":1,"V05":1,"V06":1}','20210101');

答案获取

建议你先动脑思考,动手写一写再对照看下答案,如果实在不懂可以点击下方卡片,回复:大厂sql 即可。

img
img
img

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

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

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

伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值