题解 | 贷款情况

贷款情况

https://www.nowcoder.com/practice/2817d353f0634208bcf0de74f56ca8f0

with a1 as (
select 
city ,
loan_type_id,
loan_type_name,
rank() over(partition by city order by count(*) desc ,loan_type_id) as rk
from loan_applications
join customers using(customer_id)
join loan_application_types using(application_id)
join loan_types using(loan_type_id)
group by city,loan_type_id,loan_type_name
),

a2 as(
    select city ,loan_type_name
    from a1 
    where rk =1
),

a3 as (
select 
city,
sum(loan_amount) as total_loan_amount,
round(sum(loan_amount)/count(distinct customer_id),2) as average_loan_amount,
count(distinct customer_id) as total_customers
from loan_applications
join customers using(customer_id)
join loan_application_types using(application_id)
join loan_types using(loan_type_id)
group by city )

select city,total_loan_amount,average_loan_amount,total_customers,
loan_type_name as most_applied_loan_type
from a2 
join a3 
using(city)
order by city 

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务