列表

详情


NC246102. 查询潜在贷款业务客户信息?

描述

现有某银行部分客户基础信息、账户信息、账单信息数据,如下所示:
客户基础信息表:user_tb (客户id-user_id,客户年龄-age,工作年限-work_years,年收入-annual_income)

客户账户信息表:account_tb (客户id-user_id,是否使用信用卡-credit_card,信用卡月消费额度-credit_card_limit,是否使用贷款业务-loan,贷款金额-loan_amount)
客户账单信息表:bill_tb (客户id-user_id,储蓄卡支出-savings_pay,信用卡支出-credit_pay) 支出为2021年整年总计金额

问题:现有某银行部分客户基础信息、账户信息、账单信息数据如上,目前需要拓展客户贷款业务,
需要查询出没有办理贷款的,但有使用信用卡业务并且信用卡支出占比总支出前三位的客户信息?

要求输出:客户id,年龄,年收入,信用卡额度,信用卡支出占比;
注:1、信用卡支出占比按照百分数形式输出并四舍五入保留1位小数,按照信用卡支出占比降序排序;
2、如果客户信用卡支出占比相同那么均输出,例如客户101和102信用卡支出占比并列第1,那么输出101、102及排名第2及第3的客户信息。

示例数据输出结果:

结果解析:
没有办理贷款业务并且有使用信用卡业务的客户有user_id为101、103、106、107、108共计5位客户;
5位客户信用卡支出占比分别为38.5%、44.4%45.5%、23.5%、25.0%(信用卡支出占比=信用卡支出/(储蓄卡支出+信用卡支出)),
排在前三位的是106、103、101三位客户;故结果如上所示。



示例1

输入:

drop table if exists  `user_tb` ; 
CREATE TABLE `user_tb` (
`user_id` int(11) NOT NULL,
`age`  int(11) NOT NULL,
`work_years` int(11) NOT NULL,
`annual_income` varchar(16) NOT NULL,
PRIMARY KEY (`user_id`));
INSERT INTO user_tb VALUES(101,33,10,'100000-150000'); 
INSERT INTO user_tb VALUES(102,31,6,'100000-150000'); 
INSERT INTO user_tb VALUES(103,23,1,'50000-100000'); 
INSERT INTO user_tb VALUES(104,43,15,'150000-200000'); 
INSERT INTO user_tb VALUES(105,27,2,'100000-150000'); 
INSERT INTO user_tb VALUES(106,35,15,'50000-100000'); 
INSERT INTO user_tb VALUES(107,37,15,'100000-150000'); 
INSERT INTO user_tb VALUES(108,41,20,'200000-250000'); 

drop table if exists  `account_tb` ; 
CREATE TABLE `account_tb` (
`user_id` int(11) NOT NULL,
`credit_card`  char(4) NOT NULL,
`credit_card_limit` int(11) NOT NULL,
`loan`  char(4) NOT NULL,
`loan_amount` int(11) NOT NULL,
PRIMARY KEY (`user_id`));
INSERT INTO account_tb VALUES(101,'是',10000,'否',0); 
INSERT INTO account_tb VALUES(102,'是',20000,'是',100000); 
INSERT INTO account_tb VALUES(103,'是',5000,'否',0); 
INSERT INTO account_tb VALUES(104,'否',0,'是',50000); 
INSERT INTO account_tb VALUES(105,'否',0,'否',0); 
INSERT INTO account_tb VALUES(106,'是',5000,'否',0); 
INSERT INTO account_tb VALUES(107,'是',10000,'否',0); 
INSERT INTO account_tb VALUES(108,'是',20000,'否',0); 

drop table if exists  `bill_tb` ; 
CREATE TABLE `bill_tb` (
`user_id` int(11) NOT NULL,
`savings_pay` int(11) NOT NULL,
`credit_pay` int(11) NOT NULL,
PRIMARY KEY (`user_id`));
INSERT INTO bill_tb VALUES(101,80000,50000); 
INSERT INTO bill_tb VALUES(102,90000,60000); 
INSERT INTO bill_tb VALUES(103,50000,40000); 
INSERT INTO bill_tb VALUES(104,90000,0); 
INSERT INTO bill_tb VALUES(105,100000,0); 
INSERT INTO bill_tb VALUES(106,60000,50000); 
INSERT INTO bill_tb VALUES(107,130000,40000); 
INSERT INTO bill_tb VALUES(108,150000,50000); 

输出:

user_id|age|annual_income|credit_card_limit|ratio
106|35|50000-100000|5000|45.5%
103|23|50000-100000|5000|44.4%
101|33|100000-150000|10000|38.5%

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Mysql 解法, 执行用时: 220ms, 内存消耗: 3680K, 提交时间: 2023-08-13 17:47:24

select t.user_id,t.age,t.annual_income,t.credit_card_limit,concat(round(b.credit_pay*100/(b.savings_pay+b.credit_pay), 1), '%') as ratio from (select u.user_id,u.age,u.annual_income,a.credit_card_limit from user_tb as u left join account_tb as a on u.user_id = a.user_id where a.credit_card='是' and a.loan='否') as t left join bill_tb as b on t.user_id=b.user_id order by ratio desc limit 3;

上一题