用SQL计算客户级M1回收率
时间: 2023-10-06 22:04:26 浏览: 301
M1回收率是指1-30天逾期的贷款回收金额与总逾期贷款金额的比率,因此计算客户级M1回收率的SQL语句应该包含以下几个步骤:
1. 计算每个客户的逾期贷款金额和1-30天逾期的贷款回收金额。
```
SELECT customer_id,
SUM(CASE WHEN overdue_days <= 30 THEN loan_amount ELSE 0 END) AS m1_recovered_amount,
SUM(CASE WHEN overdue_days > 0 THEN loan_amount ELSE 0 END) AS total_overdue_amount
FROM loan_table
GROUP BY customer_id;
```
2. 计算每个客户的M1回收率。
```
SELECT customer_id,
(m1_recovered_amount / total_overdue_amount) AS m1_recovery_rate
FROM (
SELECT customer_id,
SUM(CASE WHEN overdue_days <= 30 THEN loan_amount ELSE 0 END) AS m1_recovered_amount,
SUM(CASE WHEN overdue_days > 0 THEN loan_amount ELSE 0 END) AS total_overdue_amount
FROM loan_table
GROUP BY customer_id
) AS temp;
```
在以上SQL语句中,loan_table是包含贷款信息的表,包括贷款金额、逾期天数、客户ID等字段。首先使用第一条SQL语句计算每个客户的逾期贷款金额和1-30天逾期的贷款回收金额,然后使用第二条SQL语句计算每个客户的M1回收率。
阅读全文