首页 > 试题广场 >

分析客户逾期情况

[编程题]分析客户逾期情况
  • 热度指数:32401 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
有贷款信息表:loan_tb(agreement_id:合同id,customer_id:客户id,loan_amount:贷款金额,pay_amount:已还金额,overdue_days:逾期天数)
agreement_id customer_id loan_amount pay_amount overdue_days
10111 1111 20000 18000 NULL
10112 1112 10000 10000 NULL
10113 1113 15000 10000 38
10114 1114 50000 30000 NULL
10115 1115 60000 50000 NULL
10116 1116 10000 8000 NULL
10117 1117 50000 50000 NULL
10118 1118 25000 10000 5
10119 1119 20000 1000 106

客户信息表:customer_tbcustomer_id:客户id,customer_age:客户年龄,pay_ability:还款能力级别
customer_id customer_age pay_ability
1111 28 B
1112 38 A
1113 20 C
1114 30 A
1115 29 B
1116 21 C
1117 35 B
1118 36 B
1119 25 C
请根据以上数据分析各还款能力级别的客户逾期情况,按照还款能力级别统计有逾期行为客户占比。要求输出还款能力级别、逾期客户占比。
注:逾期客户占比要求按照百分数形式输出并四舍五入保留 1 位小数,最终结果按照占比降序排序。

示例数据结果如下:
pay_ability overdue_ratio
C 66.7%
B 25.0%
A 0.0%

结果解释:
还款能力级别为 C 的客户有1113、1116、1119,其中有逾期行为的客户为 1113、1119,故结果为 2/3=66.7%;其他结果同理。
示例1

输入

drop table if exists  `loan_tb` ; 
CREATE TABLE `loan_tb` (
`agreement_id` int(11) NOT NULL,
`customer_id` int(11) NOT NULL,
`loan_amount` int(11) NOT NULL,
`pay_amount` int(11) NOT NULL,
`overdue_days` int(11),
PRIMARY KEY (`agreement_id`));
INSERT INTO loan_tb VALUES(10111,1111,20000,18000,null); 
INSERT INTO loan_tb VALUES(10112,1112,10000,10000,null); 
INSERT INTO loan_tb VALUES(10113,1113,15000,10000,38); 
INSERT INTO loan_tb VALUES(10114,1114,50000,30000,null); 
INSERT INTO loan_tb VALUES(10115,1115,60000,50000,null); 
INSERT INTO loan_tb VALUES(10116,1116,10000,8000,null); 
INSERT INTO loan_tb VALUES(10117,1117,50000,50000,null); 
INSERT INTO loan_tb VALUES(10118,1118,25000,10000,5); 
INSERT INTO loan_tb VALUES(10119,1119,20000,1000,106); 

drop table if exists  `customer_tb` ; 
CREATE TABLE `customer_tb` (
`customer_id` int(11) NOT NULL,
`customer_age` int(11) NOT NULL,
`pay_ability` varchar(2) NOT NULL,
PRIMARY KEY (`customer_id`));
INSERT INTO customer_tb VALUES(1111,28,'B'); 
INSERT INTO customer_tb VALUES(1112,38,'A'); 
INSERT INTO customer_tb VALUES(1113,20,'C'); 
INSERT INTO customer_tb VALUES(1114,30,'A'); 
INSERT INTO customer_tb VALUES(1115,29,'B'); 
INSERT INTO customer_tb VALUES(1116,21,'C'); 
INSERT INTO customer_tb VALUES(1117,35,'B'); 
INSERT INTO customer_tb VALUES(1118,36,'B'); 
INSERT INTO customer_tb VALUES(1119,25,'C'); 

输出

pay_ability|overdue_ratio
C|66.7%
B|25.0%
A|0.0%
头像 牛客题解官
发表于 2025-02-18 10:15:42
精华题解 这道题目要求我们分析不同还款能力级别的客户逾期情况,计算每个还款能力级别的客户中有逾期行为的客户占比,并按占比降序排序。下面是这个SQL查询的思路和实现步骤。 1. 确定总体问题 我们需要计算每个还款能力级别的客户中有逾期行为的客户占比。逾期行为的定义是overdue_days字段不为NULL。 2 展开全文
头像 NancyFeng
发表于 2024-08-10 04:23:16
--存在同一用户有多次逾期行为所以不能直接count overdue_days --存在该用户没有贷款记录的情况 SELECT c.pay_ability, CONCAT(ROUND(100.0 * COUNT(DISTINCT 展开全文
头像 爱莉很担心你
发表于 2024-10-24 15:36:14
select pay_ability, concat(round(avg(if(overdue_days is null, 0, 1)) * 100, 1), '%') overdue_ratio from customer_tb c join loan_tb lt on 展开全文
头像 牛客238807874号
发表于 2024-08-29 16:04:01
sum(if(overdue_days is not null,1,0)) 筛选逾期人数 concat(X...,'%')用于链接X...与% select pay_ability, concat(round(sum(if(overdue_days is not null,1,0))/count( 展开全文
头像 超级塔白噜
发表于 2025-02-24 19:02:11
--先用一个case when给是否逾期打上标记,逾期记为1,非逾期记为0 with customer_info as ( select agreement_id, loan_tb.customer_id, loan_amount, 展开全文
头像 神奇的大魔王在写周报
发表于 2024-08-15 23:59:15
SELECT pay_ability, CONCAT(round(count(overdue_days) / count(1) * 100, 1),'%') as overdue_ratio FROM loan_tb l LEFT JOIN customer_tb c 展开全文
头像 七念叶
发表于 2024-08-26 14:15:15
with t1 as (select pay_ability,count(customer_id) as all_user_cnt from customer_tb group by pay_ability) ,t2 as (select a.pay_ability,a.customer_id,b 展开全文
头像 牛客468194973号
发表于 2024-12-16 20:39:17
with t1 as( select l.customer_id,l.overdue_days,c.pay_ability from loan_tb l join customer_tb c on l.customer_id = c.customer_ 展开全文
头像 有份工作就行😭
发表于 2025-03-09 00:45:52
SELECT pay_ability, CONCAT(ROUND(SUM(is_overdue)*100.0/COUNT(*), 1), '%') AS overdue_ratio FROM ( SELECT c.pay_ability, 展开全文
头像 冷艳的奶酪在看牛客
发表于 2025-02-26 10:38:48
select ct.pay_ability, concat(round(100*count(distinct case when lt.overdue_days is not Null then lt.customer_id end)/count(distinct lt.customer_id),1 展开全文
头像 哈哈士奇
发表于 2024-11-26 09:52:25
select c.pay_ability, concat( FORMAT(sum(case when coalesce(overdue_days,0)>0 then 1 else 0 end) /count(1)*100,1),'%') as overdue_ratio fro 展开全文