vintage账龄报表代码

select date(payment_date),
sum(case when period = 1 and overdue_long>8 then month_capital else null end )/sum(month_capital) mob1; --账龄1期

sum(case when (period = 1 and overdue_long>15) or (period=2 and overdue_long>8) then month_capital else null end)
/sum(month_capital) mob2; -- 账龄2期

sum(case when (period = 1 and overdue_long >22) or (period =2 and overdue_long>15) or(period = 3 and overdue_long >8)
then month_capital else null)/sum(month_capital) mob3; --账龄3期

sum(case when (period =1 and overdue_long>29) or (period = 2 and overdue_long>22) or (period =3 and overdue_long>1)
or (period =4 and overdue_long>8) then month_capital else null end)/sum(month_capital) mob4; --账龄4期

from 
(select * 
case when e.repay_status =1 then datediff(date(now()),date(e.repay_time))
	 when e.repay_status =2 and remind_end_date is null then 0
	 when e.repay_status =2 and remind_end_date is not null then datediff(date(remind_end_date),date(e.repay_time))
	 else null end overdue_long --逾期天数计算

from 
# 合同表
(select * from bor_contract where create_time >='2019-08-01 00:00:00' and period =4)a
# 还款计划表
left join
(select * from bor_installment_plan as bip) d on a.apply_id = d.apply_id
# 实际还款表
left join
(select * from huankuan) e on a.apply_id= e.apply_id and d.period=e.period
# 催收表
left join
(select * from cuishou) f on a.contract_id =f.contract_id and date(d.repay_time) = date_add(date(f.overdue_date),interval-1 day)
where 
	b.apply = 'dd' -- 产品
and b.first_again = 2 --新户,老户
and b.create_time>='2019-06-01 00:00:00' --借款时间限制
)bb
group by
date(payment_date)
order by 
date(payment_date);

猜你喜欢

转载自blog.csdn.net/weixin_42224488/article/details/111412463
今日推荐